Remove all negatives from the given Array
Given an array arr[] of size N, the task is to remove all negative elements from this array.
Examples:
Input: arr[] = {3, -4}
Output: {3}
Explanation: The only negative element of the array is -4.Input: arr[] = {1, -3, 2}
Output: {1, 2}
Approach 1: The given problem can be solved using the following steps :
- Create a vector newArr to store only positive elements.
- Now traverse the array arr, and push positive element in newArr.
- Return newArr as the answer.
Below is the implementation of the above approach:
C++
// C++ code for the above approach#include <bits/stdc++.h>using namespace std;// Function to remove the negative elementsvoid removeNegative(vector<int>& arr){ vector<int> newArr; for (auto x : arr) { if (x >= 0) { newArr.push_back(x); } } for (auto x : newArr) { cout << x << ' '; }}// Driver Codeint main(){ vector<int> arr = { 1, -3, 2 }; removeNegative(arr); return 0;} |
Java
// Java code for the above approachimport java.util.ArrayList;class GFG { // Function to remove the negative elements static void removeNegative(int[] arr) { ArrayList<Integer> newArr = new ArrayList<Integer>(); 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 elementsdef 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 Codearr = [1, -3, 2]removeNegative(arr)# This code is contributed by Taranpreet |
C#
// C# code for the above approachusing 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
<script> // JavaScript code for the above approach // Function to remove the negative elements function removeNegative(arr) { let newArr = []; for (let x of arr) { if (x >= 0) { newArr.push(x); } } for (let x of newArr) { document.write(x + " ") } } // Driver Code let arr = [1, -3, 2]; removeNegative(arr); // This code is contributed by Potta Lokesh </script> |
Output
1 2
Time Complexity: O(N)
Auxiliary Space: O(N)
Approach 2(For Java): Java’s lambda function removeIf() can be used as well to remove negative elements.
Java
// Java code for above approachimport java.util.*;public class Solution { // Function to remove the // negative elements from the array public static ArrayList<Integer> remNeg(ArrayList<Integer> arr) { arr.removeIf(n -> n < 0); return arr; } // Driver Code public static void main(String[] args) { ArrayList<Integer> arr = new ArrayList<Integer>( 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 approachusing 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<int> remNeg(List<int> arr) { arr.RemoveAll(isEven); return arr; } // Driver Code public static void Main(String[] args) { List<int> arr = new List<int>(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
<script>// javascript code for above approach // Function to remove the // negative elements from the array function remNeg( arr) { for (var i = 0; i < arr.length; i++) { if(arr[i] < 0){ arr.splice(i,1); } } return arr; } // Driver Code var arr = [1, -3, 2]; arr = remNeg(arr); for (i = 0; i < arr.length; i++) { document.write(arr[i] + " "); }// This code is contributed by Rajput-Ji</script> |
Output
1 2
Time Complexity: O(N)
Auxiliary Space: O(1)


