How to remove duplicates from ArrayList in java

An ArrayList can contain duplicate elements because each value stores in a unique index. But there may be a situation when we want only a unique element In ArrayList and want to remove duplicates from ArrayList java. There are a number of ways to remove duplicates from list in java. Let’s see how to remove duplicates from ArrayList in java

Here is the table content of the article will we will cover this topic.
1. By use of LinkedHashSet
2. By using the manual way
3. Using Java 8 Stream.distinct()

remove duplicates from ArrayList in java

By use of LinkedHashSet

As we know LinkedHashSet doesn’t contain duplicate values. So, we can remove the duplicates element from the ArrayList by converting it into LinkedHashSet.  You must think about why we are using LinkedHashSet?

Because the HashSet doesn’t maintain any order of element but LinkedHashSet maintains the insertion order of the element.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class RemoveDuplicate
{
    public static void main(String[] args) 
    {
    	ArrayList<Integer> list = new ArrayList<Integer>();
    	list.add(1);
    	list.add(2);
    	list.add(3);
    	list.add(4);
    	list.add(1);
    	list.add(3);
    	list.add(1);
    	
    	System.out.println("ArrayList element: " + list);
    	
    	LinkedHashSet<Integer> hashSet = new LinkedHashSet<Integer>(list);
    	
    	System.out.println("LinkedHashSet element: " + hashSet);
    }
}

Output: ArrayList element: [1, 2, 3, 4, 1, 3, 1]
LinkedHashSet element: [1, 2, 3, 4]

By using manual way

We can remove the duplicate element from ArrayList by use of manual way. Here we will not use any method to filter the duplicate elements.

1. We will create a new ArrayList, that will store only unique element.
2. We will transverse the ArrayList that contains duplicates element.
3. Add element in new ArrayList those are unique.

import java.util.ArrayList;
import java.util.LinkedHashSet;

public class RemoveDuplicate
{
    public static void main(String[] args) 
    {
    	ArrayList<Integer> duplicateElements = new ArrayList<Integer>();
    	duplicateElements.add(1);
    	duplicateElements.add(2);
    	duplicateElements.add(3);
    	duplicateElements.add(4);
    	duplicateElements.add(1);
    	duplicateElements.add(3);
    	duplicateElements.add(1);
    	
    	ArrayList<Integer> uniqueElements = new ArrayList<Integer>();
    	
    	for(Integer ele: duplicateElements)
    	{
    		if(!uniqueElements.contains(ele))
    			uniqueElements.add(ele);
    	}
    	
    	System.out.println("Duplicate element in ArrayList: " + duplicateElements);
    	System.out.println("Unique element in ArrayList: " + uniqueElements);
    }
}

Output: Duplicate element in ArrayList: [1, 2, 3, 4, 1, 3, 1]
Unique element in ArrayList: [1, 2, 3, 4]

Using Java 8 Stream.distinct()

We can remove the duplicate element from ArrayList by using a distinct() method of Stream. The streams are introduced in Java 8 If you are not familiar with the distinct() method please read it. The distinct() method returns a stream that contains only a unique element. This method works based on equals() method, so if you have an ArrayList contains custom object then you have to override the equals() method.

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class RemoveDuplicate
{
    public static void main(String[] args) 
    {
    	ArrayList<Integer> duplicateElements = new ArrayList<Integer>();
    	duplicateElements.add(1);
    	duplicateElements.add(2);
    	duplicateElements.add(3);
    	duplicateElements.add(4);
    	duplicateElements.add(1);
    	duplicateElements.add(3);
    	duplicateElements.add(1);
    	
    	List<Integer> newList = duplicateElements.stream().distinct().collect(Collectors.toList());
    	System.out.println("Element from ArrayList: "+ duplicateElements);
    	System.out.println("Element from new ArrayList: "+ newList);
    }
}

Output: Element from ArrayList: [1, 2, 3, 4, 1, 3, 1]
Element from new ArrayList: [1, 2, 3, 4]

2 thoughts on “How to remove duplicates from ArrayList in java”

Leave a Comment