Java Program to Remove Duplicates From an Arraylist

In the previous article, we have seen Java Program to Sort ArrayList in Descending Order

In this article we are going to see how we can remove duplicates from an ArrayList in Java.

Java Program to Remove Duplicates From an Arraylist

Let’s see different ways to remove duplicates from an ArrayList.

Method-1: Java Program to Remove Duplicates From an Arraylist By Using Set

We can remove duplicates from an arraylist using the set method in Java. Set can’t contain any duplicate elements so it will store only the unique elements from the arraylist.

Approach:

  • Create an arraylist in Java and add some elements to it.
  • Add a duplicate element
  • Print the arraylist
  • Convert the arraylist into a set.
  • Print the new list.

Program:

import java.util.*;
import java.util.stream.Collectors;
public class Main 
{
    public static void main(String[] args)
    {
        // Creating an empty ArrayList
        ArrayList<String> arr = new ArrayList<String>();
        // Adding elements to the arrayList
        arr.add("One");
        arr.add("Two");
        arr.add("Three");
        // Adding a duplicate to the list
        arr.add("Two");
        // Displaying the list
        System.out.println("Arraylist"+arr);
        // Converting the arraylist into a set
        Set <String> setList = new LinkedHashSet<String>(arr);
        // Displaying the list
        System.out.println("List(set) without duplicates"+setList);
    

    }
}

Output:

Arraylist[One, Two, Three, Two]
List(set) without duplicates[One, Two, Three]

Method-2: Java Program to Remove Duplicates From an Arraylist By Using Iterator

To remove duplicate elements using an iterator we can create another arraylist and then traverse through the arraylist and store the first occurrence of each element.

Approach:

  • Create an array list in Java and add some elements to it.
  • Add a duplicate element
  • Print the array list
  • Create another array list
  • Copy the elements from the array list to the new array list after checking if the element is not already in the arraylist. We can check by using contains( ) method.
  • Print the new list

Program:

import java.util.*;
import java.util.stream.Collectors;
public class Main 
{
    public static void main(String[] args)
    {
        // Creating an empty ArrayList
        ArrayList<String> arr = new ArrayList<String>();
        // Adding elements to the arrayList
        arr.add("One");
        arr.add("Two");
        arr.add("Three");
        // Adding a duplicate to the list
        arr.add("Two");
        // Displaying the list
        System.out.println("Arraylist"+arr);
        // Creating a second arraylist
        ArrayList<String> arr2 = new ArrayList<String>();
        for(String a : arr)
        {
            // Checks whhether the arraylist contains the element or not
            if(!arr2.contains(a))
            {
                // Adds the elements occuring once to the arraylist
                arr2.add(a);
            }
        }
        // Displaying the list
        System.out.println("Arraylist without duplicates"+arr2);
    

    }
}
Output:

Arraylist[One, Two, Three, Two]
Arraylist without duplicates[One, Two, Three]

Method-3: Java Program to Remove Duplicates From an Arraylist By Using distinct() Method

We can use the distinct method from the Java stream API to filter out only the first occurrences .

Approach:

  • Create an arraylist in Java and add some elements to it.
  • Add a duplicate element
  • Print the arraylist
  • Create another arraylist and pass original arraylist  with distinct( ) method. This will store all non duplicate elements in  the new array list.
  • Print the new list

Program:

import java.util.*;
import java.util.stream.Collectors;
public class Main 
{
    public static void main(String[] args)
    {
        // Creating an empty ArrayList
        List<String> arr = new ArrayList<String>();
        // Adding elements to the arrayList
        arr.add("One");
        arr.add("Two");
        arr.add("Three");
        // Adding a duplicate to the list
        arr.add("Two");
        // Displaying the list
        System.out.println("Arraylist"+arr);
        // Creating a second arraylist with the distinct elements from the first arraylist
        List<String> arr2 = arr.stream().distinct().collect(Collectors.toList());
        // Displaying the list
        System.out.println("Arraylist without duplicates"+arr2);
    

    }
}
Output:

Arraylist[One, Two, Three, Two]
Arraylist without duplicates[One, Two, Three]

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Related Java Programs: