How To Remove Duplicate Elements From ArrayList In Java?


ArrayList is one of the most used Collection type in java. It gives the flexibility of adding multiple null elements, duplicate elements and also maintains the insertion order of elements. While coding, you often come across the requirement where you have to remove duplicate elements from already constructed ArrayList. In this post, we will see two methods to remove duplicate elements from an ArrayList.

Method 1 : Removing Duplicate Elements From ArrayList Using HashSet

In this method, we use HashSet to remove duplicate elements from an ArrayList. As you know, HashSet doesn’t allow duplicate elements. We use this property of HashSet to remove duplicate elements from already constructed ArrayList. But, there is one disadvantage of this method. That is, it erases the insertion order of ArrayList elements. That means, after removing the duplicate elements, elements will not be in the insertion order. Let’s see one example.

import java.util.ArrayList;
import java.util.HashSet;

public class MainClass
{
	public static void main(String[] args)
	{
		//Constructing An ArrayList

		ArrayList<String> listWithDuplicateElements = new ArrayList<String>();

		listWithDuplicateElements.add("JAVA");

		listWithDuplicateElements.add("J2EE");

		listWithDuplicateElements.add("JSP");

		listWithDuplicateElements.add("SERVLETS");

		listWithDuplicateElements.add("JAVA");

		listWithDuplicateElements.add("STRUTS");

		listWithDuplicateElements.add("JSP");

		//Printing listWithDuplicateElements

		System.out.print("ArrayList With Duplicate Elements :");

		System.out.println(listWithDuplicateElements);

		//Constructing HashSet using listWithDuplicateElements

		HashSet<String> set = new HashSet<String>(listWithDuplicateElements);

		//Constructing listWithoutDuplicateElements using set

		ArrayList<String> listWithoutDuplicateElements = new ArrayList<String>(set);

		//Printing listWithoutDuplicateElements

		System.out.print("ArrayList After Removing Duplicate Elements :");

		System.out.println(listWithoutDuplicateElements);
	}
}

Output :

ArrayList With Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
ArrayList After Removing Duplicate Elements :[JAVA, SERVLETS, JSP, J2EE, STRUTS]

You notice the output of the above example. Elements are shuffled after duplicate elements are removed. They are not in the insertion order. If you want insertion order of elements to be maintained even after removing the duplicate elements, then this method is not recommended. There is another method exist which doesn’t alter the insertion order of elements even after removing the duplicate elements. That is using LinkedHashSet.

Method 2 : Removing Duplicate Elements From ArrayList Using LinkedHashSet

In this method, we use LinkedHashSet to remove duplicate elements from ArrayList. As you know that LinkedHashSet doesn’t allow duplicate elements and also maintains the insertion order of elements. Both these properties of LinkedHashSet is used here in order to remove duplicate elements from ArrayList and also maintain the insertion order of elements. See the below example.

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

public class MainClass
{
	public static void main(String[] args)
	{
		//Constructing An ArrayList

		ArrayList<String> listWithDuplicateElements = new ArrayList<String>();

		listWithDuplicateElements.add("JAVA");

		listWithDuplicateElements.add("J2EE");

		listWithDuplicateElements.add("JSP");

		listWithDuplicateElements.add("SERVLETS");

		listWithDuplicateElements.add("JAVA");

		listWithDuplicateElements.add("STRUTS");

		listWithDuplicateElements.add("JSP");

		//Printing listWithDuplicateElements

		System.out.print("ArrayList With Duplicate Elements :");

		System.out.println(listWithDuplicateElements);

		//Constructing LinkedHashSet using listWithDuplicateElements

		LinkedHashSet<String> set = new LinkedHashSet<String>(listWithDuplicateElements);

		//Constructing listWithoutDuplicateElements using set

		ArrayList<String> listWithoutDuplicateElements = new ArrayList<String>(set);

		//Printing listWithoutDuplicateElements

		System.out.print("ArrayList After Removing Duplicate Elements :");

		System.out.println(listWithoutDuplicateElements);
	}
}

Output :

ArrayList With Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
ArrayList After Removing Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, STRUTS]

Notice the output. Insertion order of elements is maintained even after the duplicate elements are removed from ArrayList.

remove duplicate elements from arraylist


15 Comments

  1. import java.io.*;
    import java.util.*;
    class Test{
    public static void main (String[] args)
    {
    String[] ar=new String[]{“shreya”,”shreya”,”poorva”,”sush”,”sush”};
    LinkedHashSet al=new LinkedHashSet(Arrays.asList(ar));
    for(String s:al){
    System.out.println(s);
    }

    }
    }

    • your code has an error on line
      for(String s:al)// err: Object cannot be converted to String

      please use generic
      LinkedHashSet al=new LinkedHashSet(Arrays.asList(ar));

  2. import java.io.*;
    import java.util.*;
    class Test{
    public static void main (String[] args)
    {
    String[] ar=new String[]{“shreya”,”shreya”,”poorva”,”sush”,”sush”};
    LinkedHashSet al=new LinkedHashSet(Arrays.asList(ar));
    for(String s:al){
    System.out.println(s);
    }
    }
    }

  3. public class DuplicateArrayList {

    public static void main(String[] args) {

    ArrayList listWithDuplicateElements = new ArrayList();
    ArrayList WoDuplicate = new ArrayList();

    listWithDuplicateElements.add(“JAVA”);

    listWithDuplicateElements.add(“J2EE”);

    listWithDuplicateElements.add(“JSP”);

    listWithDuplicateElements.add(“SERVLETS”);

    listWithDuplicateElements.add(“JAVA”);

    listWithDuplicateElements.add(“STRUTS”);

    listWithDuplicateElements.add(“JSP”);

    for(String s :listWithDuplicateElements )
    {
    if(!WoDuplicate.contains(s))
    {
    WoDuplicate.add(s);
    }
    }

    System.out.println(listWithDuplicateElements);
    System.out.println(WoDuplicate);

    }

    }

  4. public class RemoveDuplicateElementsFromArrayList {

    // Input:[JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
    // Output :[JAVA, SERVLETS, JSP, J2EE, STRUTS]
    public static void main(String args[]) {

    ArrayList ar=new ArrayList();
    ar.add(“JAVA”);
    ar.add(“J2EE”);
    ar.add(“JSP”);
    ar.add(“SERVLET”);
    ar.add(“JAVA”);
    ar.add(“STRUTS”);
    ar.add(“JSP”);

    int arrlength=ar.size();

    for ( int i=0;i<arrlength;i++)
    {

    for ( int j=i+1;j<arrlength;j++)
    {

    if(ar.get(i)==ar.get(j))
    {

    ar.set(j,"0");

    }
    }
    }
    for ( int k=0; k<arrlength;k++)
    {
    if(ar.get(k)!="0")
    {
    System.out.print(ar.get(k)+ " ");
    }
    }

    }

    }

Leave a Reply