Remove all occurrences of specific element from an array in Java
This post will discuss how to remove all occurrences of the specified element from a primitive integer array in Java.
We know that arrays in Java are not dynamic, unlike an ArrayList, and one cannot simply remove an element from it. However, one can create a new array from the original array containing the desired elements and return it. This post provides an overview of some of the available alternatives to accomplish this.
1. Using Java 8 Stream
The recommended approach is to use the Stream for this task. The idea is first to convert the given array into the stream and filter all occurrences of the specified element using the filter() method. Finally, we convert the stream back to an array using the toArray() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; class Main { public static int[] remove(int[] a, int key) { return Arrays.stream(a) // IntStream .filter(val -> val != key) .toArray(); } public static void main(String[] args) { int[] a = { 1, 2, 3, 2, 4, 2, 4, 5 }; int key = 2; a = remove(a, key); System.out.println(Arrays.toString(a)); } } |
Output:
[1, 3, 4, 4, 5]
2. Using ArrayList
Another plausible way of removing all occurrences of the specified element from the specified array involves using a list data structure.
- Create an empty ArrayList.
- Insert all array elements into the list except the specified key.
- Convert the list back to an array and return it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static int[] remove(int[] a, int key) { // create an empty `ArrayList` List<Integer> result = new ArrayList<>(); // insert all elements from the array into the list // except the specified key for (int i: a) { if (i != key) { result.add(i); } } // convert the list back to an array and return it return result.stream() .mapToInt(Integer::intValue) .toArray(); } public static void main(String[] args) { int[] a = { 1, 2, 3, 2, 4, 2, 4, 5 }; int key = 2; a = remove(a, key); System.out.println(Arrays.toString(a)); } } |
Output:
[1, 3, 4, 4, 5]
Here’s another alternative approach using a list:
- Insert all elements from the array into a list.
- Remove all occurrences of the specified key from the list using its
removeAll()method. - Convert the list back to an array and return it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static int[] remove(int[] a, int key) { List<Integer> result = IntStream.of(a) // IntStream .boxed() .collect(Collectors.toList()); result.removeAll(Arrays.asList(key)); return result.stream() .mapToInt(Integer::intValue) .toArray(); } public static void main(String[] args) { int[] a = { 1, 2, 3, 2, 4, 2, 4, 5 }; int key = 2; a = remove(a, key); System.out.println(Arrays.toString(a)); } } |
Output:
[1, 3, 4, 4, 5]
3. Using Apache Commons Lang
Another good alternative is to leverage Apache Commons ArrayUtils class, which provides the removeAllOccurences() method for this purpose.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; class Main { public static void main(String[] args) { int[] a = { 1, 2, 3, 2, 4, 2, 4, 5 }; int key = 2; a = ArrayUtils.removeAllOccurences(a, key); System.out.println(Arrays.toString(a)); } } |
That’s all about removing all occurrences of the specified element from an array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)