Remove Duplicates from ArrayList in Java

In this tutorial, we will learn how to remove duplicate elements from an ArrayList in Java.

Let’s know something about ArrayList in Java.

ArrayList is defined as a dynamic array in Java that is present in the Collection Framework of java.util package. It works quite a bit more flexibly than arrays but it is slower than arrays. It is very helpful when manipulation of data in the array is needed.

Approaches to remove Duplicates from ArrayList in Java

There are many methods to remove the duplicate element in the ArrayList in Java. In this article, we will learn three of those methods.

Method  1: Using only ArrayList

The first method is a simple approach to removing the duplicate elements.

1: Create an ArrayList arr with duplicate elements.

2: Create an empty ArrayList new_arr of the same type as the previous one.

3: Traverse the ArrayList arr and check whether the ArrayList new_arr has the same element in it or not. If the condition is false i.e, the indicated element is new then add it to the new_arr or else continue.

4: Print the new_arr ArrayList and you will see that no duplicate element is present.

CODE:

import java.io.*;
import java.util.*;
class arrayListDuplication{

    public static void main(String[] args)throws IOException{
        
        ArrayList<Integer> arr=new ArrayList<Integer>(Arrays.asList(1,2,3,3,4,1,1,4,5,5,6,7,2,3,5,7,6,6,6));
        //Create a new ArrayList
        ArrayList<Integer> new_arr=new ArrayList<Integer>();

        for(int i=0;i<arr.size();i++){
            //if the new_arr has any element which is same then it add it only once
            if(new_arr.contains(arr.get(i))==false)
            new_arr.add(arr.get(i));
        }

        //Original Array
        System.out.println("Original Array -> "+ arr);

        //New Array
        System.out.println("\nNew Array with No duplicate Elements -> "+new_arr+"\n");
    }
}

OUTPUT:

Original Array -> [1, 2, 3, 3, 4, 1, 1, 4, 5, 5, 6, 7, 2, 3, 5, 7, 6, 6, 6]

New Array with no Duplicate elements -> [1 ,2 ,3 ,4 ,5 ,6 ,7]

Method  2: Using LinkedHashset

In this method, we will use linkedHashset. But why do we need to use this because Set allows only unique element inside itself and no duplicates is added in it.

1: Create an ArrayList arr with duplicate elements.

2: Create an empty linked HashSet new_arr of the same type as the previous one.

3: use the addall() function to add all the elements in the ArrayList arr to Set new_arr. According to the Property LinkedHashSet creates a unique set in it.

Step 4: Clear the ArrayList arr and add the element of the Set into the arrayList.

Step 5: Print the ArrayList and you will see that the elements are all unique.

CODE:

import java.io.*;
import java.util.*;
class arrayListDuplication{

    public static void main(String[] args)throws IOException{
        
        ArrayList<Integer> arr=new ArrayList<Integer>(Arrays.asList(1,2,3,3,4,1,1,4,5,5,6,7,2,3,5,7,6,6,6));
        //Original Array
        System.out.println("Original Array -> "+ arr);
        //Create a new ArrayList
        Set<Integer> new_arr=new LinkedHashSet<>();
        new_arr.addAll(arr);
        arr.clear();
        arr.addAll(new_arr);
        //New Array
        System.out.println("\nNew Array with No duplicate Elements -> "+new_arr+"\n");
    }
}

OUTPUT:

Original Array -> [1, 2, 3, 3, 4, 1, 1, 4, 5, 5, 6, 7, 2, 3, 5, 7, 6, 6, 6]

New Array with no Duplicate elements -> [1 ,2 ,3 ,4 ,5 ,6 ,7]

Method 3: Using HashMap

The last method we will be talking about is of using HashMap. By using the hashmap will create a key-value pair and counter the duplicate elements and then print only the keys.

1: Create an ArrayList arr with duplicate elements.

2: Create an empty Hashmap map (key value pair).

3: Traverse the ArrayList and add the element in the hashmap. So if the element is new then create a new key or else counter the value of the existing key.

4: Traverse the HashMap and print the key from the HashMap.

CODE:

import java.io.*;
import java.util.*;
import java.util.Map.Entry;
class arrayListDuplication{

    public static void main(String[] args)throws IOException{
        
        ArrayList<Integer> arr=new ArrayList<Integer>(Arrays.asList(1,2,3,3,4,1,1,4,5,5,6,7,2,3,5,7,6,6,6));
        //Original Array
        System.out.println("Original Array -> "+ arr);
        //Create a new ArrayList
       HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
       for(int i=0;i<arr.size();i++){
           if(map.containsKey(arr.get(i))==false){
               map.put(arr.get(i),1);
           }
           else{
               int d=map.get(arr.get(i));
               d++;
               map.put(arr.get(i),d);
           }
       }
       System.out.print("\nNew Array with no Duplicate elements -> ");
       for (Entry<Integer, Integer> e : map.entrySet()){
           System.out.print(e.getKey()+" ");
           
       }
       System.out.println("\n");
    }
}

OUTPUT:

Original Array -> [1, 2, 3, 3, 4, 1, 1, 4, 5, 5, 6, 7, 2, 3, 5, 7, 6, 6, 6]

New Array with no Duplicate elements -> 1 2 3 4 5 6 7

So these were the 3 methods by which we can remove duplicate elements from the arrayList. Hope you like my explaination.

Happy Coding !!!

Leave a Reply

Your email address will not be published.