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.

Download  Run Code

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:

Download  Run Code

Output:

[1, 2, 4, 5]

 
The above code is equivalent to the following non-Stream version using for-loop:

Download  Run Code

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.

Download  Run Code

Output:

[1, 2, 4, 5]

That’s all about removing duplicates from an array in Java.