Skip to main content

Remove duplicates from ArrayList in Java

How to remove duplicates from ArrayList in Java

In this article, we are going to learn how we can remove duplicates from ArrayList in Java using different approaches. If you don't know about ArrayList, read this article first: All About ArrayList in Java.

Content of this article

How to remove duplicates from ArrayList in Java - Using Another ArrayList?

In this approach, we need to traverse the ArrayList, read this article to learn: Different ways to iterate ArrayList in Java

Steps to remove duplicates from ArrayList in Java - Using Another ArrayList:

  1. Create another ArrayList.
  2. Traverse through each element of Original ArrayList.
  3. If the element is not present in the new ArrayList, then add element into the new ArrayList.

Example 1: Remove duplicates from ArrayList in Java - Using Another ArrayList

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

public class ArrayListExample {
     public static void main(String[] args) {
          ArrayList<String> languages = new ArrayList<>(
               Arrays.asList("Java", "C++", "Java", "Python", "C++", "Python", ".net"));
          System.out.println("All Languages : " + languages);

          ArrayList<String> uniqueLanguages = removeDuplicatesFromArrayList(languages);
          System.out.println("Unique Languages : " + uniqueLanguages);
     }
     private static <T> ArrayList<T> removeDuplicatesFromArrayList(ArrayList<T> arrayList) {
          ArrayList<T> uniqueList = new ArrayList<>();
          for (T element : arrayList) {
               if (!uniqueList.contains(element)) {
                    uniqueList.add(element);
               }
          }
          return uniqueList;
     }
}
Output
All Languages : [Java, C++, Java, Python, C++, Python, .net]
Unique Languages : [Java, C++, Python, .net]

How to remove duplicates from ArrayList in Java - Using LinkedHashSet?

We can use LinkedHashSet to remove duplicates from ArrayList in Java as LinkedHashSet cannot store duplicate elements and also maintain the same insertion order as of the ArrayList.

Steps to remove duplicates from ArrayList in Java - Using LinkedHashSet:

  1. Create an object of LinkedHashSet.
  2. Add all elements of ArrayList into the LinkedHashSet.
  3. Create an ArrayList from LinkedHashSet.

Example 2: Remove duplicates from ArrayList in Java - Using LinkedHashSet

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

public class ArrayListExample {
     public static void main(String[] args) {
          ArrayList<String> languages = new ArrayList<>(
               Arrays.asList("Java", "C++", "Java", "Python", "C++", "Python", ".net"));
          System.out.println("All Languages : " + languages);

          ArrayList<String> uniqueLanguages = removeDuplicatesFromArrayList(languages);
          System.out.println("Unique Languages : " + uniqueLanguages);
     }
     private static <T> ArrayList<T> removeDuplicatesFromArrayList(ArrayList<T> arrayList) {
          LinkedHashSet<T> linkedHashSet = new LinkedHashSet<>();
          linkedHashSet.addAll(arrayList);
          ArrayList<T> uniqueList = new ArrayList<>(linkedHashSet);
          return uniqueList;
     }
}
Output
All Languages : [Java, C++, Java, Python, C++, Python, .net]
Unique Languages : [Java, C++, Python, .net]

How to remove duplicates from ArrayList in Java - Using Stream APIs?

We can use stream APIs to remove duplicates from ArrayList in Java. To do this, we need to use stream(), distinct() and collect() methods of Java Stream API.
  • stream() : returns a stream of the ArrayList.
  • distinct() : returns a stream of distinct or unique elements from a stream.
  • collect() : collect the elements of stream into the specified Collector.

Example 3: Remove duplicates from ArrayList in Java - Using Stream APIs

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<String> languages = new ArrayList<>(
               Arrays.asList("Java", "C++", "Java", "Python", "C++", "Python", ".net"));
          System.out.println("All Languages : " + languages);

          ArrayList<String> uniqueLanguages = removeDuplicatesFromArrayList(languages);
          System.out.println("Unique Languages : " + uniqueLanguages);
     }
     private static <T> ArrayList<T> removeDuplicatesFromArrayList(ArrayList<T> arrayList) {
          List<T> uniqueList = arrayList.stream().distinct().collect(Collectors.toList());
          return new ArrayList<>(uniqueList);
     }
}
Output
All Languages : [Java, C++, Java, Python, C++, Python, .net]
Unique Languages : [Java, C++, Python, .net]

Related Posts:

Conslusion

In this article, we have learned different ways to remove duplicates from ArrayList in Java. We have learned 3 approaches Using Another ArrayList, Using LinkedHashSet and Using Stream APIs to remove duplicates from ArrayList in Java.

Comments