Java ProgramsCollectionsArrayList Remove Duplicates

ArrayList Remove Duplicates in Java

beginner·  Collections  ·  List

Problem

A LinkedHashSet stores only unique elements and remembers insertion order, which makes wrapping an ArrayList in one a quick way to drop duplicates without scrambling the list.

Given an ArrayList with repeated values, produce a new list containing each value only once, in its original order.

Input
[Apple, Banana, Apple, Cherry, Banana]
Output
[Apple, Banana, Cherry]

Java Program

Java
import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; public class ArrayListRemoveDuplicates { public static void main(String[] args) { ArrayList<String> names = new ArrayList<>(); names.add("Apple"); names.add("Banana"); names.add("Apple"); names.add("Cherry"); names.add("Banana"); // LinkedHashSet drops duplicates automatically, keeping first-seen order List<String> unique = new ArrayList<>(new LinkedHashSet<>(names)); System.out.println(unique); } }

Output

[Apple, Banana, Cherry]

Core Logic

Feeding the list straight into a LinkedHashSet's constructor collapses every duplicate automatically, and converting the result back to a list restores a list-shaped return type.

How It Works
  1. 1new LinkedHashSet<>(names) copies every element from names into the set, silently skipping any value already present.
  2. 2Unlike a plain HashSet, a LinkedHashSet keeps track of the order elements were first inserted in, so nothing gets shuffled.
  3. 3new ArrayList<>(uniqueSet) copies the deduplicated set's contents back into a fresh ArrayList.
  4. 4The final list holds the same elements as the original, minus every repeat, in their original first-seen order.
For [Apple, Banana, Apple, Cherry, Banana], the set keeps only the first Apple and first Banana, producing [Apple, Banana, Cherry].
💡

Key Point: Choosing LinkedHashSet over a plain HashSet is what preserves the original order — a plain HashSet would still remove duplicates correctly, but the resulting order would be unspecified.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Building the LinkedHashSet visits each of the n elements once, with an O(1) average-case check-and-insert per element, and the set holds up to n distinct elements.

Key Concepts

LinkedHashSetArrayListinsertion order

Approach 2: Java 8

Java
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class ArrayListRemoveDuplicatesStream { public static void main(String[] args) { ArrayList<String> names = new ArrayList<>(); names.add("Apple"); names.add("Banana"); names.add("Apple"); names.add("Cherry"); names.add("Banana"); // distinct() drops repeats while streaming, keeping first-seen order List<String> unique = names.stream() .distinct() .collect(Collectors.toList()); System.out.println(unique); } }

Output

[Apple, Banana, Cherry]

Core Logic

Stream's distinct() drops repeated elements while streaming, so filtering and collecting in one pipeline reproduces the same deduplicated, order-preserving result without an intermediate Set at all.

How It Works
  1. 1names.stream() opens a stream over the original list, duplicates included.
  2. 2.distinct() keeps only the first occurrence of each element, using equals() to detect repeats — later duplicates are silently dropped.
  3. 3.collect(Collectors.toList()) gathers the surviving elements into a new List.
  4. 4Like distinct()'s LinkedHashSet-based cousin, encounter order is preserved — the first occurrence of each value keeps its original position.
Streaming [Apple, Banana, Apple, Cherry, Banana] through distinct() keeps only the first Apple and first Banana, producing [Apple, Banana, Cherry].
💡

Key Point: distinct() reaches the same result as the LinkedHashSet technique without ever naming a Set — it's the more declarative choice when deduplication is just one step in a larger stream pipeline.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: distinct() internally tracks seen elements (typically backed by a hash set) to check each of the n elements once, and the resulting list holds up to n distinct elements.

Key Concepts

Streamdistinct()Collectors.toList()

Related Programs