How to Remove Duplicates from ArrayList in Java

By Preeti Jain Last updated : February 05, 2024

Problem statement

Given an ArrayList, write a Java program to remove duplicates from the given ArrayList.

Removing Duplicates from ArrayList in Java

Below are the two approaches to remove duplicates from an ArrayList:

  1. Using HashSet
  2. Using LinkedHashSet

Remove Duplicates from ArrayList in Java Using HashSet

  • The HashSet class is available in java.util package.
  • By using HashSet class, we can remove the duplicate element from the ArrayList.
  • In case of HashSet, after removing the duplicate elements, The insertion order of the elements is not preserved (i.e. The retrieval order of the elements is not needed to be the same as the insertion order).

Steps

The steps of removing duplicates from ArrayList using HashSet:

  • Copy ArrayList elements to HashSet.
  • After copying, clear ArrayList by using the clear() method.
  • Again copy HashSet elements to ArrayList.

Example to Remove Duplicates from ArrayList in Java Using HashSet

// Java program to demonstrate the example of
// removing duplicate element from ArrayList 
// by using HashSet.

import java.util.*;

public class RemovedDuplicateFromArrayList {
    public static void main(String[] args) {
        // ArrayList Declaration
        ArrayList al = new ArrayList();

        // By using add() method to add few elements in 
        // ArrayList
        al.add(10);
        al.add(10);
        al.add(20);
        al.add(20);
        al.add(30);

        // Display ArrayList with duplicates
        System.out.print("Display ArrayList with duplicates : " + " ");
        System.out.println(al);

        // HashSet Declaration
        HashSet hs = new HashSet();

        // By using addAll() method is to add all elements
        // to HashSet

        hs.addAll(al);

        // By using clear() method is to clear the ArrayList
        al.clear();

        // Again by using addAll() method is to add all elements
        // to ArrayList
        al.addAll(hs);

        // Display ArrayList with no duplicates
        System.out.print("Display ArrayList with no duplicates : " + " ");
        System.out.println(al);
    }
}

The output of the above example is:

Display ArrayList with duplicates :  [10, 10, 20, 20, 30]
Display ArrayList with no duplicates :  [20, 10, 30]

Remove Duplicates from ArrayList in Java Using LinkedHashSet

  • This LinkedHashSet class is available in java.util package.
  • By using LinkedHashSet class, we can remove the duplicate element from the ArrayList.
  • In the case of LinkedHashSet, after removing the duplicate elements, The insertion order of the elements is preserved (i.e. The retrieval order of the elements is needed to be the same as the insertion order).

Steps

The Steps of removing duplicates from ArrayList using LinkedHashSet

  • Copy ArrayList elements to LinkedHashSet.
  • After copying, clear ArrayList by using the clear() method.
  • Again copy LinkedHashSet elements to ArrayList.

Example to Remove Duplicates from ArrayList in Java Using LinkedHashSet

// Java program to demonstrate the example of
// removing duplicate element from ArrayList 
// by using LinkedHashSet.

import java.util.*;

public class RemovedDuplicateFromArrayList {
    public static void main(String[] args) {
        // ArrayList Declaration
        ArrayList al = new ArrayList();

        // By using add() method to add few elements in 
        // ArrayList
        al.add(10);
        al.add(10);
        al.add(20);
        al.add(20);
        al.add(30);

        // Display ArrayList with duplicates
        System.out.print("Display ArrayList with duplicates : " + " ");
        System.out.println(al);

        // LinkedHashSet Declaration
        LinkedHashSet lhs = new LinkedHashSet();

        // By using addAll() method is to add all elements
        // to LinkedHashSet
        lhs.addAll(al);

        // By using clear() method is to clear the ArrayList
        al.clear();

        // Again by using addAll() method is to add all elements
        // to ArrayList
        al.addAll(lhs);

        // Display ArrayList with no duplicates
        System.out.print("Resultant ArrayList : " + " ");
        System.out.println(al);
    }
}

The output of the above example is:

Display ArrayList with duplicates :  [10, 10, 20, 20, 30]
Resultant ArrayList :  [10, 20, 30]

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.