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

Popular posts from this blog

How HashMap works internally in java

How Hashmap works Internally in Java In this article, we are going to see how HashMap internally works in java. Also, we will have a look at what Java 8 made changes on the internal working of Hashmap to make it faster. What is Hashmap and Hashing in java? How Hashmap works Internally in Java is majorly dependent upon the Hashing Principle. So, Before going to learn how HashMap works internally in java, lets first understand what is HashMap and hashing. HashMap : A HashMap is a map used to store mappings of key-value pairs. Also, it works on the Principle of Hashing. To know more about the HashMap, visit this article: HashMap in Java Hashing Principle : Simply, Hashing is a method used to produce an integer value from an object and this integer value known as the hash value. In HashMap, the key object is used for Hashing.
Internal Struture of the HashMap in java For internal working of HashMap, HashMap maintains an array of bucket, each bucket is a linked-list and linked list is a li…

HashMap in Java

HashMap in Java In this article, we are going to learn about the most important class of Java known as HashMap. HashMap in java is the most used map interface and it is used in our regular or day-to-day programming practices.
What is HashMap in java? Lets first start with basic definition of HashMap,
HashMap is a map which store key-value pair, where each key is mapped with a value.
HashMap defined in java.util package and implements map interface which provides functionalities to perform basic operation like store, retrieve, size of the map etc. It is a part of Java Collection Framework. Time complexity of HashMap in Java? : Time complexity to store and retrieve data from the HashMap is O(1) in the Best Case. But it can be O(n) in the worst case and after the changes made in Java 8 the worst case time complexity can be O(log n) atmost. Internal working of HashMap in java HashMap maintains an array of the buckets, where each bucket is a linked-list and the linked list is a list of…

ArrayList in Java

ArrayList in Java In this article, we are going to discuss ArrayList in Java. ArrayList is a class from the Java Collection Framework and widely in our programs. We use ArrayList as a substitute for the array. So, it is important to know ArrayList in detail. We are going to see what is ArrayList in Java, how we can create the instance of ArrayList and the methods provided by the ArrayList in Java.

If you already familiar to ArrayList, you may learn common programs related to ArrayList.
Table of ContentsWhat is ArrayList in Java?Hierarchy of the ArrayList in java?How to create objects of ArrayList in Java?How to create an ArrayList of Custom Objects?How to iterate ArrayList in Java?How to get the elements by index from ArrayList in Java?How to check whether an elements is stored in the ArrayList in Java?How to remove elements from the ArrayList in Java?Few more methods of ArrayList in Java?
What is ArrayList in Java? ArrayList in Java is a List in which we can store and retrieve elemen…