Removing all occurrences of a specified element from an array means creating a new array that contains only those elements that are different from the given key.
Example:
Input:
arr[] = {3, 9, 2, 3, 1, 7, 2, 3, 5},key = 3
Output:[9, 2, 1, 7, 2, 5]Input:
arr[] = {10, 20, 10, 30, 50, 10},key = 10
Output:[20, 30, 50]
Different Approaches to Remove All Occurrences
1. Using Arrays.copyOf()
We overwrite the original array with elements that are different from the specified key. After that, Arrays.copyOf() creates a new array containing only the valid elements.
import java.util.Arrays;
public class Geeks {
// Removes all occurrences of key from the array
public static int[] removeElements(int[] arr, int key) {
int index = 0;
// Copy elements that are not equal to key
for (int i = 0; i < arr.length; i++) {
if (arr[i] != key) {
arr[index++] = arr[i];
}
}
// Create an array containing only the remaining elements
return Arrays.copyOf(arr, index);
}
public static void main(String[] args) {
int[] arr = {3, 9, 2, 3, 1, 7, 2, 3, 5};
int key = 3;
int[] result = removeElements(arr, key);
System.out.println(Arrays.toString(result));
}
}
Output
[9, 2, 1, 7, 2, 5]
Explanation
- index keeps track of the position where the next valid element should be stored.
- The loop checks every element of the array.
- If the element is not equal to key, it is copied to the beginning of the array.
- Arrays.copyOf(arr, index) creates a new array containing only the remaining elements.
2. Using Java Streams
Java Streams provide a concise way to filter out all occurrences of the specified element.
Approach
- Convert the array into a stream.
- Use
filter()to keep elements that are different from the key. - Convert the filtered stream back into an array.
import java.util.Arrays;
public class Geeks {
public static void main(String[] args) {
Integer[] arr = {3, 9, 2, 3, 1, 7, 2, 3, 5};
int key = 3;
Integer[] result = Arrays.stream(arr)
.filter(element -> element != key)
.toArray(Integer[]::new);
System.out.println(Arrays.toString(result));
}
}
Output
[9, 2, 1, 7, 2, 5]
Explanation
- Arrays.stream(arr) creates a stream from the array.
- filter(element -> element != key) removes the elements equal to key.
- toArray(Integer[]::new) converts the filtered stream back into an Integer[].
3. Using ArrayList and removeAll()
An array can be converted into an ArrayList, which supports dynamic removal of elements.
Approach
- Convert the array into a modifiable ArrayList.
- Use removeAll() to remove every occurrence of the key.
- Convert the list back into an array.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Geeks {
public static void main(String[] args) {
Integer[] arr = {3, 9, 2, 3, 1, 7, 2, 3, 5};
Integer key = 3;
// Convert array to a modifiable list
List<Integer> list = new ArrayList<>(Arrays.asList(arr));
// Remove all occurrences of key
list.removeAll(Arrays.asList(key));
// Convert list back to an array
Integer[] result = list.toArray(new Integer[0]);
System.out.println(Arrays.toString(result));
}
}
Output
[9, 2, 1, 7, 2, 5]
Explanation
- Arrays.asList(arr) converts the Integer[] into a list.
- new ArrayList<>(...) creates a modifiable list.
- removeAll(Arrays.asList(key)) removes every occurrence of key.
- toArray() converts the resulting list back into an array.
4. Using List.removeIf()
The removeIf() method removes all elements that satisfy a given condition. It is useful when the removal condition needs to be expressed using a predicate.
Approach
- Convert the array into a modifiable ArrayList.
- Use removeIf() to remove elements equal to the key.
- Convert the list back into an array.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Geeks {
public static void main(String[] args) {
Integer[] arr = {3, 9, 2, 3, 1, 7, 2, 3, 5};
Integer key = 3;
// Convert array to a modifiable list
List<Integer> list = new ArrayList<>(Arrays.asList(arr));
// Remove all elements equal to key
list.removeIf(element -> element.equals(key));
// Convert list back to an array
Integer[] result = list.toArray(new Integer[0]);
System.out.println(Arrays.toString(result));
}
}
Output
[9, 2, 1, 7, 2, 5]
Explanation
- The array is converted into a modifiable ArrayList.
- removeIf() checks each element against the specified condition.
- element.equals(key) returns true for elements that should be removed.
- The remaining elements are converted back into an array.