How to Remove Duplicates from Arraylist in Java?

Overview
To remove duplicates from an ArrayList in Java, create a new empty ArrayList to store unique elements. Then, iterate through the original ArrayList, checking if each element already exists in the new ArrayList. If not, add it to the new ArrayList. This way, the new ArrayList will contain unique elements, effectively removing duplicates. In this article, we will learn How to Remove Duplicates from ArrayList in Java.
Given an ArrayList with duplicate values, the task is to remove the duplicate values from this ArrayList in Java.
Examples:
Let's see an example to remove duplicates from ArrayList:
Output:
Using Iterator to Remove Duplicates from ArrayList
Approach
Get the list of duplicate values in the array. Make a new ArrayList. Use the includes() method to iterate through the first ArrayList and store each element's first occurrence in the second ArrayList. The elements that have had the duplicates deleted are in the second ArrayList.
The implementation of the above approach is shown below:
Code
Output
Using LinkedHashSet to Remove Duplicates from ArrayList
Converting an ArrayList into a Set that prevents duplicates is a more effective technique to eliminate duplicates (both in terms of time complexity and ease of implementation). Therefore, LinkedHashSet is the best solution since it prevents duplication and maintains insertion order.
Approach
- Get the list of duplicate values in the array.
- Take this ArrayList and turn it into a LinkedHashSet. It will do so to eliminate duplication.
- This LinkedHashSet should be changed back to ArrayList.
- The elements that have had the duplicates deleted are in the second ArrayList.
The implementation of the above approach can be seen below:
Code
Output
Using Java Stream.distinct() Method to Remove Duplicates from ArrayList
The Stream API's distinct() method is available for use. Based on the result produced by the equals() method, the distinct() method returns a new Stream without duplicate entries that can be utilized for additional processing. The Stream pipeline's actual processing begins once terminal methods like forEach() or collect() are called.
Approach
- Get the list of duplicate values in the array.
- From this ArrayList, make a new List.
- Using Stream().distinct() method, you can get a unique object stream.
- Object stream to List conversion
Below is the implementation of the above approach:
Code
Output
FAQs
Q. How can I remove duplicates from an ArrayList in Java?
A. To remove duplicates from an ArrayList in Java, you can iterate through the list and add elements to a new ArrayList if they haven't been added before. Alternatively, you can use Java 8 streams to achieve the same result using the distinct() method.
Q. Can you show an example of removing duplicates from an ArrayList using Java 8 streams?
A. Certainly! Here's an example using Java 8 streams:
Q. Is there any performance impact when removing duplicates from a large ArrayList?
A. Removing duplicates from a large ArrayList can impact performance, especially if you use the traditional iteration approach, as it has a time complexity of . Using Java 8 streams with distinct() is generally more efficient with an O(n) time complexity.
Q. What happens to the order of elements when duplicates are removed from an ArrayList?
A. When using the traditional approach to remove duplicates, the order of elements is preserved. In the case of Java 8 streams, the order is also preserved, as distinct() maintains the original order of elements.
Conclusion
- In conclusion, removing duplicates from an ArrayList in Java can be achieved efficiently using traditional iteration or Java 8 streams with the distinct() method.
- The order of elements is preserved, and custom objects can remove duplicates by implementing appropriate equals and hashCode methods.
- While there are no standard Java libraries for this purpose, third-party libraries are available.
- Using sets or streams offers more efficient solutions than nested loops for duplicate removal.