How do I remove repeated elements from ArrayList?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
To remove duplicate values from an ArrayList in Java, the best method depends on whether you need to preserve the original order. The most common practical answer is to use a LinkedHashSet, because it removes duplicates and keeps insertion order.
The Quickest Order-Preserving Solution
If order matters, convert the list to a LinkedHashSet and then back to a list:
Output:
This is usually the cleanest answer because it keeps the first occurrence of each value and removes later repeats.
If Order Does Not Matter
If you only care about uniqueness and do not care about element order, a plain HashSet is enough:
This may print the values in a different order, because HashSet does not preserve insertion order.
Java Streams
In Java 8 and later, stream().distinct() is a concise alternative:
distinct() preserves encounter order for sequential streams, so it behaves similarly to the LinkedHashSet approach.
In-Place Removal Versus Creating a New List
Most examples create a new collection. That is usually fine and keeps the code readable. If you really need to mutate the existing list object, you can clear it and add the unique values back:
That preserves the same ArrayList instance while removing duplicates.
Equality Rules Matter
Duplicate removal depends on equals() and hashCode(). For built-in types such as String and Integer, this usually works as expected. For custom objects, you must implement those methods correctly.
Without correct equality logic, two objects that look like duplicates to a human may still be treated as distinct by the set.
Common Pitfalls
- Using
HashSetwhen order matters often surprises people because the result order can change. - Forgetting that duplicate removal relies on
equals()andhashCode()breaks the solution for custom object types. - Writing a manual nested-loop removal routine is usually slower and less readable than using set-based approaches.
- Assuming
stream().distinct()changes the original list in place is incorrect; it produces a new stream result. - Removing while iterating over the same
ArrayListmanually can lead to skipped elements orConcurrentModificationExceptiondepending on the approach.
Summary
- The usual order-preserving solution is
new ArrayList<>(new LinkedHashSet<>(list)). - Use
HashSetonly when order is irrelevant. - '
stream().distinct()is a concise modern alternative.' - For custom objects, duplicate removal depends on correct
equals()andhashCode()implementations. - Prefer clear set-based solutions over manual duplicate-removal loops unless you have a very specific reason not to.
Related reading
- How do I remove the first item from a list?
- How do I remove the first item from a list?
- How do I return dictionary keys as a list in Python?
- How do I return dictionary keys as a list in Python?
- How do I resolve ClassNotFoundException?
- How do I resolve the java.net.BindException Address already in use JVM_Bind error?
- How do I return the index of the target element in a Python array?
- How do I reverse a list or loop over it backwards?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.