Remove all occurrences of an element from Array in Java
Last Updated :
22 Oct, 2024
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]
Program to Remove all occurrences of an element from Array
Below is the implementation using Arrays.copyOf of above Program:
Java
// Java program remove all occurrences
// of an element from Array using naive method
import java.util.Arrays;
class GFG {
// 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 index = 0;
for (int i=0; i<a.length; i++)
if (a[i] != k)
a[index++] = a[i];
// Create a copy of arr[]
return Arrays.copyOf(a, index);
}
// 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));
}
}
Complexity of the above method:
Time Complexity: O(n)
Space Complexity: O(n)
Other Methods to Remove all occurrences of an element from Array
1. Using Java 8 Stream
Approach to Implement the Method:
- Get the array and the key.
- Filter all element of the list which is equal to a given key
- Convert the list back to an array and return it.
Program Implementation for the Approach:
Java
// Java program remove all occurrences
// of an element from Array using Stream
import java.util.Arrays;
public class RemoveElement {
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[] newArr = Arrays.stream(a)
.filter(e -> e != k) // Filter out the element to remove
.toArray(Integer[]::new); // Collect result into a new array
// Print the result
System.out.println(Arrays.toString(newArr));
}
}
OutputArray after removal: [9, 2, 1, 7, 2, 5]
2. Using Java ArrayList
Approach to Implement the ArrayList Method:
- 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.
Alternate Approach:
- First Create a List of Array.
- Remove all elements of the array into the list that are the specified key.
- Convert the list back to an array and return it.
Program Implementation for the Approach:
Java
// 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 RemoveElementUsingArrayList {
// 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> list = new ArrayList<>(Arrays.asList(a));
// Remove all occurrences of the element
list.removeAll(Arrays.asList(k));
// Convert ArrayList back to array
Integer[] newArr = list.toArray(new Integer[0]);
// Print the result
System.out.println(Arrays.toString(newArr));
}
}
OutputArray after removal: [9, 2, 1, 7, 2, 5]
3. Using List.removeAll()
- First Create an empty List of Array.
- Insert all elements of the array into the list
- Remove all elements which is you want to remove
- Convert the list back to an array and return it.
Program Implementation for the Approach:
Java
// Java program remove all occurrences
// of an element from Array using List.removeAll
import java.util.*;
// Driver Class
public class RemoveElementUsingRemoveAll {
// 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> list = new ArrayList<>(Arrays.asList(a));
// Remove all occurrences of the element
list.removeAll(Arrays.asList(k));
// Convert List back to array
Integer[] newArr = list.toArray(new Integer[0]);
// Print the result
System.out.println(Arrays.toString(newArr));
}
}
4. Using List.removeIf()
- First Create an empty List of Array.
- Insert all elements of the array into the list
- Remove all those element which is you want to remove using the equals() method
- Convert the list back to an array and return it.
Program Implementation for the Approach:
Java
// Java program remove all occurrences
// of an element from Array using removeIf()
import java.util.*;
// Driver Class
public class RemoveElementUsingRemoveIf {
// 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> list = new ArrayList<>(Arrays.asList(a));
// Remove all occurrences using removeIf()
list.removeIf(e -> e == k);
// Convert List back to array
Integer[] newArr = list.toArray(new Integer[0]);
// Print the result
System.out.println(Arrays.toString(newArr));
}
}