Remove duplicates from an array in Java
This post will discuss how to remove duplicates from an array in Java.
We know that an array can hold a fixed number of items and its length is decided on the creation and cannot be changed. Therefore, removing elements from an array is not possible, but you can create a new array containing the distinct elements. There are many options to achieve this in Java:
1. Using Stream.distinct() method
In Java 8 and above, the recommended solution is to use Stream API to create a new array without duplicates. The idea is to construct a stream from the array elements and get the distinct elements in the stream with the distinct() method. Then call the toArray() method to accumulate the stream elements into a new array.
The following example demonstrates this. Note that the solution preserves the relative order of elements.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; public class Main { public static int[] removeDuplicates(int[] arr) { return Arrays.stream(arr).distinct().toArray(); } public static void main(String[] args) { int[] arr = {2, 4, 1, 2, 1, 2, 4, 5}; int[] distinct = removeDuplicates(arr); System.out.println(Arrays.toString(distinct)); } } |
Output:
[2, 4, 1, 5]
2. Convert To Set
The idea here is to insert all array elements into a set and then convert the set back into an array. This works since the Set data structure doesn’t support duplicates. However, this approach destroys the ordering of the array elements. To preserve the original order, use the Stream.distinct() method discussed above.
This is demonstrated below using Java 8 Stream:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.Set; import java.util.stream.Collectors; public class Main { public static int[] removeDuplicates(int[] arr) { Set<Integer> set = Arrays.stream(arr).boxed().collect(Collectors.toSet()); return set.stream().mapToInt(Integer::intValue).toArray(); } public static void main(String[] args) { int[] arr = {2, 4, 1, 2, 1, 2, 4, 5}; int[] distinct = removeDuplicates(arr); System.out.println(Arrays.toString(distinct)); } } |
Output:
[1, 2, 4, 5]
The above code is equivalent to the following non-Stream version using for-loop:
|
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 |
import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class Main { public static int[] removeDuplicates(int[] arr) { Set<Integer> set = new HashSet<>(); for (int i : arr) { set.add(i); } int[] result = new int[set.size()]; int k = 0; for (int i: set) { result[k++] = i; } return result; } public static void main(String[] args) { int[] arr = {2, 4, 1, 2, 1, 2, 4, 5}; int[] distinct = removeDuplicates(arr); System.out.println(Arrays.toString(distinct)); } } |
Output:
[1, 2, 4, 5]
3. Sorting
Another solution is to sort the given array in ascending order and compare every pair of consecutive elements to identify and eliminate the duplicates. Finally, copy the unique elements into a new array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; public class Main { public static int[] removeDuplicates(int[] arr) { Arrays.sort(arr); int k = 0; for (int i = 0; i < arr.length; i++) { if (i == 0 || arr[i] != arr[i - 1]) { arr[k++] = arr[i]; } } return Arrays.copyOf(arr, k); } public static void main(String[] args) { int[] arr = {2, 4, 1, 2, 1, 2, 4, 5}; int[] distinct = removeDuplicates(arr); System.out.println(Arrays.toString(distinct)); } } |
Output:
[1, 2, 4, 5]
That’s all about removing duplicates 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 :)