Home » Java programming language

How to remove duplicates from ArrayList in Java?

Removing duplicates from ArrayList: Here, we are going to learn how to remove duplicates (duplicate records) from an ArrayList in Java programming language?
Submitted by Preeti Jain, on September 13, 2019

Removing duplicates from ArrayList

To remove duplicates from an ArrayList, there are two ways,

  1. With the help of HashSet
  2. With the help of LinkedHashSet

1) Remove duplicates using HashSet

  • This 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).

The process of removing duplicate elements 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:

// 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);
    }
}

Output

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

2) Remove duplicates using LinkedHashSet

  • This 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).

The process of removing duplicate elements 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:

// 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);
    }
}

Output

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.