Remove specific element from an array in Java
This post will discuss how to remove a specific element from an array in Java.
Arrays in Java have fixed lengths. This means they hold a fixed number of values of a single type. The length of an array is decided upon its creation. After creation, its length is fixed.
Since the array length is fixed, there is no standard way to remove elements from it. However, you can create a new array containing all the original array elements except the one which you want to remove. There are several ways to achieve that in Java:
1. Using Apache Commons Lang Library
The Apache Commons Lang’s ArrayUtils class offers the removeElement() method to remove the first occurrence of the specified element from the specified array. It is overloaded to accept all primitive types and object arrays. Following is a simple example demonstrating its usage.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; public class Main { public static void main(String[] args) { int[] arr = {2, 5, 7, 3, 4, 8, 9}; int item = 4; arr = ArrayUtils.removeElement(arr, item); System.out.println(Arrays.toString(arr)); } } |
Output:
[2, 5, 7, 3, 8, 9]
2. Using Java 8
You can also leverage Stream API to remove an element from an array. The idea is to convert the array into a sequential stream, filter the stream to remove the given element, and accumulate the remaining elements into a new array using a collector.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; public class Main { public static int[] removeElement(int[] arr, int item) { return Arrays.stream(arr) .filter(i -> i != item) .toArray(); } public static void main(String[] args) { int[] arr = {2, 5, 7, 3, 4, 8, 9}; int item = 4; arr = removeElement(arr, item); System.out.println(Arrays.toString(arr)); } } |
Output:
[2, 5, 7, 3, 8, 9]
Here’s a working example for an array of objects, which uses the equals() method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; public class Main { public static String[] removeElement(String[] arr, String item) { return Arrays.stream(arr) .filter(s -> !s.equals(item)) .toArray(String[]::new); } public static void main(String[] args) { String[] arr = {"A", "C", "B", "C", "D"}; String item = "C"; arr = removeElement(arr, item); System.out.println(Arrays.toString(arr)); } } |
Output:
[A, B, D]
3. Convert To List
Another solution is to convert the array into a list and then remove the given element using List’s remove() method. Finally, convert the list back to the array of the same type. This solution is not recommended as it involves the creation of an intermediary list object.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { public static String[] removeElement(String[] arr, String item) { List<String> list = new ArrayList<>(Arrays.asList(arr)); list.remove(item); return list.toArray(String[]::new); } public static void main(String[] args) { String[] arr = {"A", "B", "C", "D"}; String item = "C"; arr = removeElement(arr, item); System.out.println(Arrays.toString(arr)); } } |
Output:
[A, B, D]
In case you need to remove all instances of the specific element, you can use List’s removeAll() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { public static String[] removeElement(String[] arr, String item) { List<String> list = new ArrayList<>(Arrays.asList(arr)); list.removeAll(Arrays.asList(item)); return list.toArray(String[]::new); } public static void main(String[] args) { String[] arr = {"A", "C", "B", "C", "D"}; String item = "C"; arr = removeElement(arr, item); System.out.println(Arrays.toString(arr)); } } |
Output:
[A, B, D]
4. Remove by index
If you have the index of the element to be removed, you can use the System.arraycopy() method to ensure better performance. The idea is to allocate a new array of size one less than the original array. Then, you can make two calls to the System.arraycopy() to copy the array elements into the new array before and after that index.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Arrays; public class Main { public static String[] removeElement(String[] arr, int index) { String[] result = new String[arr.length - 1]; System.arraycopy(arr, 0, result, 0, index); if (arr.length != index) { System.arraycopy(arr, index + 1, result, index, arr.length - index - 1); } return result; } public static void main(String[] args) { String[] arr = {"A", "C", "B", "C", "D"}; int index = 1; arr = removeElement(arr, index); System.out.println(Arrays.toString(arr)); } } |
Output:
[A, B, C, D]
That’s all about removing specific elements 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 :)