How to Remove Duplicates from ArrayList in Java

Last Updated : 26 Aug, 2026

An ArrayList in Java allows duplicate elements. Sometimes, we need to remove these duplicate values while keeping only one occurrence of each element. Java provides several ways to remove duplicates from an ArrayList, such as using the contains() method, LinkedHashSet, and the Stream API.

  • Using contains() is simple but can be less efficient for large lists.
  • LinkedHashSet removes duplicates and maintains insertion order.

Illustration:

Input: ArrayList = [1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5]
Output: ArrayList after removing duplicates = [1, 10, 2, 3, 4, 5]

Input: ArrayList = [Apple, Banana, Apple, Mango, Banana, Orange]
Output: ArrayList after removing duplicates = [Apple, Banana, Mango, Orange]

Different Approaches to Remove Duplicates

1. Using contains() Method

This approach traverses the list and adds only the first occurrence of each element to a new list.

Java
import java.util.*;
public class GFG {
    public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list) {
        ArrayList<T> newList = new ArrayList<>();
        for (T element : list) {
            if (!newList.contains(element)) {
                newList.add(element);
            }
        }
        return newList;
    }

    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>(
            Arrays.asList(1, 10, 1, 2, 2, 3, 3, 10, 3, 4, 5, 5)
        );

        System.out.println("ArrayList with duplicates: " + list);
        ArrayList<Integer> newList = removeDuplicates(list);
        System.out.println("ArrayList with duplicates removed: " + newList);
    }
}

Output
ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 3, 10, 3, 4, 5, 5]
ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]

Explanation:

  • if (!newList.contains(element)) ensures only the first occurrence is added.
  • The original list remains unchanged, and newList contains all elements without duplicates

2. Using LinkedHashSet

This code removes duplicates from an ArrayList by adding only the first occurrence of each element to a new list.

Java
import java.util.*;
public class GFG {

    public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list) {
        Set<T> set = new LinkedHashSet<>(list);
        list.clear();
        list.addAll(set);
        return list;
    }

    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>(
            Arrays.asList(1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5)
        );

        System.out.println("ArrayList with duplicates: " + list);
        ArrayList<Integer> newList = removeDuplicates(list);
        System.out.println("ArrayList with duplicates removed: " + newList);
    }
}

Output
ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5]
ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]

Explanation:

  • if (!newList.contains(element)) ensures only the first occurrence is added.
  • The original list remains unchanged, and newList contains all elements without duplicates.

3. Using Java 8 Stream API

This code shows how to remove duplicates from an ArrayList using Java 8 Stream API’s distinct() method.

Java
import java.util.*;
import java.util.stream.Collectors;

public class GFG {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>(
            Arrays.asList(1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5)
        );

        System.out.println("ArrayList with duplicates: " + list);

        List<Integer> newList = list.stream()
                                    .distinct()
                                    .collect(Collectors.toList());

        System.out.println("ArrayList with duplicates removed: " + newList);
    }
} 
Try It Yourself
redirect icon

Output
ArrayList with duplicates: [1, 10, 1, 2, 2, 3, 10, 3, 3, 4, 5, 5]
ArrayList with duplicates removed: [1, 10, 2, 3, 4, 5]

Explanation:

  • distinct() ensures only unique elements are retained.
  • collect(Collectors.toList()) converts the distinct elements back into a List.
Comment