How to Remove Duplicates from ArrayList in Java With Example

ArrayList is the most popular implementation of the List interface from Java’s Collection framework, but it allows duplicates. Though there is another collection called Set which is primarily designed to store unique elements, there are situations when you receive a List like ArrayList in your code and you need to ensure that it doesn’t contain any duplicate before processing. Since with ArrayList you cannot guarantee uniqueness, there is no other choice but to remove repeated elements from ArrayList. 
There are multiple ways to do this, you can follow the approach we used for removing duplicates from array in Java, where we loop through array and inserting each element in a Set, which ensures that we discard duplicate because Set doesn’t allow them to insert, or you can also use remove method of ArrayList to get rid of them, once you found that those are duplicates.

Btw, the simplest approach to remove repeated objects from ArrayList is to copy them to a Set e.g. HashSet and then copy it back to ArrayList. This will remove all duplicates without writing any more code.

One thing to noted is that, if original order of elements in ArrayList is important for you, as List maintains insertion order, you should use LinkedHashSet because HashSet doesn’t provide any ordering guarantee.

If you are using deleting duplicates while iterating, make sure you use Iterator’s remove() method and not the ArrayList one to avoid ConcurrentModificationException.  In this tutorial we will see this approach to remove duplicates.

Java Program to Remove duplicates from ArrayList

Here is our sample program to learn how to remove duplicates from ArrayList. The steps followed in the below example are:

  • Copying all the elements of ArrayList to LinkedHashSet. Why we choose LinkedHashSet? Because it removes duplicates and maintains the insertion order.
  • Emptying the ArrayList, you can use clear() method to remove all elements of ArrayList and start fresh. 
  • Copying all the elements of LinkedHashSet (non-duplicate elements) to the ArrayList. 

You can further join Java Programming Masterclass for a Software Engineers course to learn more about the ArrayList class and different algorithms to remove duplicate objects. 
Please find below the complete code :

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

 * Java Program to remove repeated elements from ArrayList in Java.
 * @author WINDOWS 8

public class ArrayListDuplicateDemo{
    public static void main(String args[]){
        // creating ArrayList with duplicate elements
        List<Integer> primes = new ArrayList<Integer>();
        primes.add(7);  //duplicate
        // let's print arraylist with duplicate
        System.out.println("list of prime numbers : " + primes);
        // Now let's remove duplicate element without affecting order
        // LinkedHashSet will guaranteed the order and since it's set
        // it will not allow us to insert duplicates.
        // repeated elements will automatically filtered.
        Set<Integer> primesWithoutDuplicates
                   = new LinkedHashSet<Integer>(primes);
        // now let's clear the ArrayList so that we can 
        // copy all elements from LinkedHashSet
        // copying elements but without any duplicates
        System.out.println("list of primes without duplicates : " + primes);

list of prime numbers : [2, 3, 5, 7, 7, 11]
list of primes without duplicates : [2, 3, 5, 7, 11]

In this example, you can see we have created an ArrayList and added numbers into it, all prime numbers. We added ‘7’ twice, so that it become duplicate. Now we print the ArrayList and you can see that it contains number 7 twice.

How to remove duplicates from ArrayList in Java

After that we created a LinkedHashSet from our ArrayList, clear our original ArrayList and then added all elements from set to the list. This time we should not have any duplicates because Set doesn’t allow them and they should have filtered when elements copied from ArrayList to HashSet by Java. This is proved by printing the ArrayList again, now it doesn’t contain 7 twice, but only once.

That’s all about how to remove duplicates from ArrayList in Java. Though there are multiple ways to do this, I think using LinkedHashSet is the simplest one because its simple and also preserve the order of elements.

1 thought on “How to Remove Duplicates from ArrayList in Java With Example”


Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!