How To Remove Duplicate Elements From ArrayList In Java?

As we know that ArrayList allows adding duplicate elements to it. In this article, we write a program to remove duplicate elements from ArrayList using different approaches.

Three ways we can write logic to remove duplicate elements from ArrayList:
  1. Using HashSet
  2. Using Java 8 lambda Expression
  3. Using LinkedHashSet(maintains order)

Using HashSet

As we know that the HashSet class contains only unique values and don't allow duplicate values to add it. We simply convert ArrayList to HashSet.
As we know that the HashSet class contains only unique values and don't allow duplicate values to add it. We simply convert ArrayList to HashSet.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class RemoveDuplicatesFromArrayList {
 public static void main(final String[] args) {
  
  final List<Integer> listWithDuplicates = new ArrayList<>();
  
  listWithDuplicates.add(10);
  listWithDuplicates.add(20);
  listWithDuplicates.add(30);
  listWithDuplicates.add(30);
  listWithDuplicates.add(50);
  listWithDuplicates.add(50);
  listWithDuplicates.add(10);
  
  System.out.println("Before removing duplicates :: 
                                 " + Arrays.toString(listWithDuplicates.toArray()));
  
         final List<Integer> listWithoutDuplicates = new ArrayList<>(
         new HashSet<>(listWithDuplicates));
     
        System.out.println("After removing duplicates :: "
                                  + Arrays.toString(listWithoutDuplicates.toArray()));
 }
}
Output:
Before removing duplicates :: [10, 20, 30, 30, 50, 50, 10]
After removing duplicates :: [50, 20, 10, 30]

Using Java 8 lambda Expression

public class RemoveDuplicatesFromArrayList {
 public static void main(final String[] args) {
  
  final List<Integer> listWithDuplicates = new ArrayList<>();
  
  listWithDuplicates.add(10);
  listWithDuplicates.add(20);
  listWithDuplicates.add(30);
  listWithDuplicates.add(30);
  listWithDuplicates.add(50);
  listWithDuplicates.add(50);
  listWithDuplicates.add(10);
  
  System.out.println("Before removing duplicates :: "
                                  + Arrays.toString(listWithDuplicates.toArray()));
  
   final List<Integer> listWithoutDuplicates = listWithDuplicates.stream()
        .distinct()
        .collect(Collectors.toList());
     System.out.println("After removing duplicates :: "
                               + Arrays.toString(listWithoutDuplicates.toArray()));
 }
}
Output:
Before removing duplicates :: [10, 20, 30, 30, 50, 50, 10]
After removing duplicates :: [10, 20, 30, 50]

Using LinkedHashSet(maintains order)

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

public class RemoveDuplicatesFromArrayList {
 public static void main(final String[] args) {
  
  final List<Integer> listWithDuplicates = new ArrayList<>();
  
  listWithDuplicates.add(10);
  listWithDuplicates.add(20);
  listWithDuplicates.add(30);
  listWithDuplicates.add(30);
  listWithDuplicates.add(50);
  listWithDuplicates.add(50);
  listWithDuplicates.add(10);
  
  System.out.println("Before removing duplicates :: "
                + Arrays.toString(listWithDuplicates.toArray()));
  
   final List<Integer> listWithoutDuplicates = new ArrayList<>(
         new LinkedHashSet<>(listWithDuplicates));
        System.out.println("After removing duplicates :: " 
               + Arrays.toString(listWithoutDuplicates.toArray()));
     
 }
}
Output:
Before removing duplicates :: [10, 20, 30, 30, 50, 50, 10]
After removing duplicates :: [10, 20, 30, 50]


Comments