How to remove duplicates from arraylist in java

Let’s learn how to remove duplicates from arraylist in java.

How to remove duplicates from arraylist in java

Here the task is to remove duplicates in Arraylist. In the below java program first get given ArrayList with duplicate integer values. Now print ArrayList with duplicate integer values on the console.

Create another ArrayList ‘alNew’. Pass first arraylist as an argument to removeDuplicate() method. In this method traverse first arraylist elements and store first arrival of each element into second arraylist using contains() method.

After traversing arraylist elements using foreach loop second arraylist contains elements with duplicates removed.

import java.util.ArrayList;
import java.util.Arrays;
public class DuplicatesFromArraylist
{
   public static <T> ArrayList<T> removeDuplicate(ArrayList<T> al)
   {
      ArrayList<T> alNew = new ArrayList<T>();
      for(T element : al)
      {
         // if element is present in alNew then add it
         if(!alNew.contains(element))
         {
            alNew.add(element);
         }
      }
      // return alNew
      return alNew;
   }
   public static void main(String[] args)
   {
      ArrayList<Integer> al = new ArrayList<>(Arrays.asList(2, 2, 4, 4, 6, 6, 8, 8, 1, 3, 5, 7, 9));
      System.out.println("Before removing duplicates from ArrayList: " + al);
      ArrayList<Integer> alNew = removeDuplicate(al);
      // printing ArrayList with duplicates
      System.out.println("After removing duplicates from ArrayList: " + alNew);
   }
}


Output:

Before removing duplicates from ArrayList: [2, 2, 4, 4, 6, 6, 8, 8, 1, 3, 5, 7, 9]
After removing duplicates from ArrayList: [2, 4, 6, 8, 1, 3, 5, 7, 9]


remove duplicates from array java 8

To remove duplicates from array in java 8 use distinct() method. distinct() method returns a stream consisting of the distinct elements (according to Object.equals(Object)) of given stream.

In the below java program first get ArrayList with duplicate values. Using this arraylist create new list. Now using Stream().distinct() method return distinct object stream. Finally convert object stream into List. Let’s see an example.

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class RemoveDuplicatesJava8
{
   public static void main(String[] args)
   {
      List li1 = new ArrayList<>(Arrays.asList(2, 2, 4, 4, 6, 6, 8, 8, 1, 3, 5, 7, 9));
      System.out.println("Before removing duplicate elements: " + li1);
      // create new list from elements of original list
      List li2 = li1.stream().distinct().collect(Collectors.toList());
      // printing ArrayList with duplicates removed
      System.out.println("After removing duplicate elements: " + li2);
   }
}


Output:

ArrayList with duplicates: [2, 2, 4, 4, 6, 6, 8, 8, 1, 3, 5, 7, 9]
ArrayList with duplicates removed: [2, 4, 6, 8, 1, 3, 5, 7, 9]


Similarly let’s remove duplicates from ArrayList using LinkedHashSet. Here we have to first convert ArrayList to Set. Because a Set do not allow duplicates.

In the below java program first get ArrayList with duplicate values. Now create LinkedHashSet from given ArrayList. Here LinkedHashSet will remove all duplicate values.

In the next step convert LinkedHashSet back to ArrayList. Finally print second ArrayList on the console with duplicate values removed.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
public class UsingLinkedHashSet
{
   public static <T>ArrayList<T> removeDuplicate(ArrayList<T> al)
   {
      Set<T> set = new LinkedHashSet<>();
      set.addAll(al);
      // clear list
      al.clear();
      al.addAll(set);
      return al;
   }
   public static void main(String[] args)
   {
      // ArrayList with duplicate values
      ArrayList<Integer> al1 = new ArrayList<>(Arrays.asList(2, 2, 4, 4, 6, 6, 8, 8, 1, 3, 5, 7, 9));
      System.out.println("ArrayList with duplicate values: " + al1);
      ArrayList<Integer> al2 = removeDuplicate(al1);
      // printing ArrayList with duplicates removed
      System.out.println("ArrayList with duplicate values removed: " + al2);
   }
}


Output:

ArrayList with duplicate values: [2, 2, 4, 4, 6, 6, 8, 8, 1, 3, 5, 7, 9]
ArrayList with duplicate values removed: [2, 4, 6, 8, 1, 3, 5, 7, 9]