Google News
logo
Java Collections - Interview Questions
How to remove duplicates from ArrayList?
There are two ways to remove duplicates from the ArrayList.
 
Using HashSet : By using HashSet we can remove the duplicate element from the ArrayList, but it will not then preserve the insertion order.
Using LinkedHashSet : We can also maintain the insertion order by using LinkedHashSet instead of HashSet.

The Process to remove duplicate elements from ArrayList using the LinkedHashSet:
 
* Copy all the elements of ArrayList to LinkedHashSet.
* Now copy all the elements of LinkedHashset to ArrayList.
* Empty the ArrayList using clear() method, which will remove all the elements from the list.
Advertisement