Backend Development 6 min read

Five Methods to Remove Duplicates from a Java ArrayList

This article presents five distinct techniques for eliminating duplicate elements from a Java ArrayList, including using LinkedHashSet, Java 8 Stream distinct, HashSet with order preservation, manual contains checks, and a double‑for‑loop approach, each accompanied by complete code examples and output results.

Architecture Digest
Architecture Digest
Architecture Digest
Five Methods to Remove Duplicates from a Java ArrayList

The article demonstrates five different ways to delete duplicate entries from a Java ArrayList , providing both explanation and runnable code for each method.

1. Using LinkedHashSet – A LinkedHashSet removes duplicates while preserving insertion order. The example creates a list of integers, converts it to a LinkedHashSet , then back to a list.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;

public class ArrayListExample {
    public static void main(String[] args) {
        ArrayList
numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8));
        System.out.println(numbersList);
        LinkedHashSet
hashSet = new LinkedHashSet<>(numbersList);
        ArrayList
listWithoutDuplicates = new ArrayList<>(hashSet);
        System.out.println(listWithoutDuplicates);
    }
}

Output:

[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]

2. Using Java 8 Stream distinct() – The Stream API’s distinct() method returns a stream of unique elements, which can be collected back into a list.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ArrayListExample {
    public static void main(String[] args) {
        ArrayList
numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8));
        System.out.println(numbersList);
        List
listWithoutDuplicates = numbersList.stream().distinct().collect(Collectors.toList());
        System.out.println(listWithoutDuplicates);
    }
}

Output:

[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]

3. Using HashSet with order check – Because a plain HashSet does not guarantee order, this method adds elements to a HashSet only if they are not already present, building a new list that keeps the original order.

private static void removeDuplicate(List
list) {
    HashSet
set = new HashSet
(list.size());
    List
result = new ArrayList
(list.size());
    for (String str : list) {
        if (set.add(str)) {
            result.add(str);
        }
    }
    list.clear();
    list.addAll(result);
}

4. Using List.contains() – Iterate through the original list and add each element to a new list only if the new list does not already contain it.

private static void removeDuplicate(List
list) {
    List
result = new ArrayList
(list.size());
    for (String str : list) {
        if (!result.contains(str)) {
            result.add(str);
        }
    }
    list.clear();
    list.addAll(result);
}

5. Double for‑loop removal – A nested loop compares each pair of elements and removes the duplicate when found.

for (int i = 0; i < list.size(); i++) {
    for (int j = 0; j < list.size(); j++) {
        if (i != j && list.get(i) == list.get(j)) {
            list.remove(list.get(j));
        }
    }
}

Each technique offers a trade‑off between simplicity, performance, and order preservation, allowing developers to choose the most suitable approach for their specific use case.

JavaCollectionsStreamArrayListHashSetDuplicate RemovalLinkedHashSet
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.