从给定的数组中删除所有负数
给定一个大小为N的数组arr[] ,任务是从该数组中删除所有负元素。
例子:
Input: arr[] = {3, -4}
Output: {3}
Explanation: The only negative element of the array is -4.
Input: arr[] = {1, -3, 2}
Output: {1, 2}
方法 1 :给定问题可以使用以下步骤解决:
- 创建一个向量newArr以仅存储正元素。
- 现在遍历数组arr ,并将正元素压入newArr 。
- 返回newArr作为答案。
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
using namespace std;
// Function to remove the negative elements
void removeNegative(vector& arr)
{
vector newArr;
for (auto x : arr) {
if (x >= 0) {
newArr.push_back(x);
}
}
for (auto x : newArr) {
cout << x << ' ';
}
}
// Driver Code
int main()
{
vector arr = { 1, -3, 2 };
removeNegative(arr);
return 0;
}
Java
// Java code for the above approach
import java.util.ArrayList;
class GFG {
// Function to remove the negative elements
static void removeNegative(int[] arr) {
ArrayList newArr = new ArrayList();
for (int x : arr) {
if (x >= 0) {
newArr.add(x);
}
}
for (int x : newArr) {
System.out.print(x + " ");
}
}
// Driver Code
public static void main(String args[]) {
int[] arr = { 1, -3, 2 };
removeNegative(arr);
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# Python code for the above approach
# Function to remove the negative elements
def removeNegative(arr):
newArr = []
for x in range(0, len(arr)):
if (arr[x] >= 0):
newArr.append(arr[x])
for x in range(0, len(newArr)):
print(newArr[x], end=' ')
# Driver Code
arr = [1, -3, 2]
removeNegative(arr)
# This code is contributed by Taranpreet
C#
// C# code for the above approach
using System;
using System.Collections;
class GFG {
// Function to remove the negative elements
static void removeNegative(int[] arr) {
ArrayList newArr = new ArrayList();
foreach (int x in arr) {
if (x >= 0) {
newArr.Add(x);
}
}
foreach (int x in newArr) {
Console.Write(x + " ");
}
}
// Driver Code
public static void Main() {
int[] arr = { 1, -3, 2 };
removeNegative(arr);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
Java
// Java code for above approach
import java.util.*;
public class Solution {
// Function to remove the
// negative elements from the array
public static ArrayList
remNeg(ArrayList arr)
{
arr.removeIf(n -> n < 0);
return arr;
}
// Driver Code
public static void main(String[] args)
{
ArrayList arr
= new ArrayList(
Arrays.asList(1, -3, 2));
arr = remNeg(arr);
for (int i = 0; i < arr.size(); i++) {
System.out.print(arr.get(i) + " ");
}
}
}
C#
// C# code for above approach
using System;
using System.Collections.Generic;
public class Solution {
static bool isEven(int i)
{
return i < 0;
}
// Function to remove the
// negative elements from the array
static List
remNeg(List arr)
{
arr.RemoveAll(isEven);
return arr;
}
// Driver Code
public static void Main(String[] args)
{
List arr
= new List(new int[]{1, -3, 2});
arr = remNeg(arr);
for (int i = 0; i < arr.Count; i++) {
Console.Write(arr[i] + " ");
}
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
1 2
时间复杂度: 在)
辅助空间: 在)
方法 2(对于Java): Java 的 lambda函数removeIf()也可用于删除否定元素。
Java
// Java code for above approach
import java.util.*;
public class Solution {
// Function to remove the
// negative elements from the array
public static ArrayList
remNeg(ArrayList arr)
{
arr.removeIf(n -> n < 0);
return arr;
}
// Driver Code
public static void main(String[] args)
{
ArrayList arr
= new ArrayList(
Arrays.asList(1, -3, 2));
arr = remNeg(arr);
for (int i = 0; i < arr.size(); i++) {
System.out.print(arr.get(i) + " ");
}
}
}
C#
// C# code for above approach
using System;
using System.Collections.Generic;
public class Solution {
static bool isEven(int i)
{
return i < 0;
}
// Function to remove the
// negative elements from the array
static List
remNeg(List arr)
{
arr.RemoveAll(isEven);
return arr;
}
// Driver Code
public static void Main(String[] args)
{
List arr
= new List(new int[]{1, -3, 2});
arr = remNeg(arr);
for (int i = 0; i < arr.Count; i++) {
Console.Write(arr[i] + " ");
}
}
}
// This code is contributed by shikhasingrajput
Javascript
输出
1 2
时间复杂度: 在)
辅助空间: O(1)