Remove all Occurrences of an Element from Array in Java
In Java, removing all occurences of a given element from an array can be done using different approaches that are,
- Naive approach using array copy
- Java 8 Streams
- Using ArrayList.removeAll()
- Using List.removeIf()
Problem Stament: Given an array and a key, the task is to remove all occurrences of the specified key from the array in Java.
Examples to Remove Elements Occurrences in Array:
Input: array = { 3, 9, 2, 3, 1, 7, 2, 3, 5 }, key = 3
Output: [9, 2, 1, 7, 2, 5]
Input: array = { 10, 20, 10, 30, 50, 10 }, key = 10
Output: [20, 30, 50]
Illustration: In this example, we will remove all occurrences of an element from an array using the Arrays.copyOf method.
// Java program remove all occurrences
// of an element from Array using naive method
import java.util.Arrays;
class Geeks
{
// function to remove all occurrences
// of an element from an array
public static int[] removeElements(int[] a, int k)
{
// Move all other elements to beginning
int ind = 0;
for (int i=0; i<a.length; i++)
if (a[i] != k)
a[ind++] = a[i];
// Create a copy of arr[]
return Arrays.copyOf(a, ind);
}
// Driver code
public static void main(String[] args)
{
int[] a = { 3, 9, 2, 3, 1, 7, 2, 3, 5 };
int k = 3;
a = removeElements(a, k);
System.out.println(Arrays.toString(a));
}
}
Output
[9, 2, 1, 7, 2, 5]
Complexity of the above method:
- Time Complexity: O(n)
- Space Complexity: O(n)
Java Program to Remove All Occurrences of an Element from Array
1. Using Java 8 Stream
Approach:
- Get the array and the key.
- Filter all elements of the list which is equal to a given key
- Convert the list back to an array and return it.
Illustration: Here, we will use the Java 8 streams to remove all occurrences of an element from an array.
// Java program remove all occurrences
// of an element from Array using Stream
import java.util.Arrays;
public class Geeks
{
public static void main(String[] args) {
// Example array
Integer[] a = { 3, 9, 2, 3, 1, 7, 2, 3, 5 };
// Element to be removed
int k = 3;
// Remove all occurrences of the element using Java 8 Stream
Integer[] ans = Arrays.stream(a)
// Filter out the element to remove
.filter(e -> e != k)
.toArray(Integer[]::new);
System.out.println(Arrays.toString(ans));
}
}
Output
[9, 2, 1, 7, 2, 5]
2. Using Java ArrayList
Approach:
- Get the array and the key.
- Create an empty ArrayList.
- Insert all elements from the array into the list except the specified key
- Convert the list back to an array and return it.
Alternative Approach:
- First, create a List of arrays.
- Remove all elements of the array into the list that are the specified key.
- Convert the list back to an array and return it.
Illustration: Using the ArrayList to remove all occurrences of an element from an array.
// Java program remove all occurrences
// of an element from Array using ArrayList
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// Driver Class
public class Geeks {
// Main Method
public static void main(String[] args) {
Integer[] a = { 3, 9, 2, 3, 1, 7, 2, 3, 5 };
// Element to be removed
Integer k = 3;
// Convert array to ArrayList
List<Integer> l = new ArrayList<>(Arrays.asList(a));
// Remove all occurrences of the element
l.removeAll(Arrays.asList(k));
// Convert ArrayList back to array
Integer[] newArr = l.toArray(new Integer[0]);
// Print the result
System.out.println(Arrays.toString(newArr));
}
}
Output
[9, 2, 1, 7, 2, 5]
3. Using List.removeAll()
Approach:
- First, create a list from the array.
- Insert all elements of the array into the list
- Remove all elements that match the given key.
- Convert the list back to an array and return it.
Illustration: Using the List.removeAll() to remove all occurrences of an element from an array.
// Java program remove all occurrences
// of an element from Array using List.removeAll
import java.util.*;
// Driver Class
public class Geeks
{
// Main Method
public static void main(String[] args) {
// Example array
Integer[] a = { 3, 9, 2, 3, 1, 7, 2, 3, 5 };
// Element to be removed
Integer k = 3;
// Convert array to List (using ArrayList to allow modification)
List<Integer> l = new ArrayList<>(Arrays.asList(a));
// Remove all occurrences of the element
l.removeAll(Arrays.asList(k));
// Convert List back to array
Integer[] ans = l.toArray(new Integer[0]);
// Print the result
System.out.println(Arrays.toString(ans));
}
}
Output
[9, 2, 1, 7, 2, 5]
4. Using List.removeIf()
Approach:
- First Create an empty List of Array.
- Insert all elements of the array into the list.
- Remove all those elements which is you want to remove using the equals() method.
- Convert the list back to an array and return it.
Illustration: Using the List.removeIf() to remove all occurrences of an element from an array.
// Java program remove all occurrences
// of an element from Array using removeIf()
import java.util.*;
// Driver Class
public class Geeks
{
// Main Method
public static void main(String[] args) {
// Example array
Integer[] a = { 3, 9, 2, 3, 1, 7, 2, 3, 5 };
// Element to be removed
int k = 3;
// Convert array to List (using ArrayList to allow modification)
List<Integer> l = new ArrayList<>(Arrays.asList(a));
// Remove all occurrences using removeIf()
l.removeIf(e -> e == k);
// Convert List back to array
Integer[] ans = l.toArray(new Integer[0]);
System.out.println(Arrays.toString(ans));
}
}
Output
[9, 2, 1, 7, 2, 5]