Java – Remove duplicate elements from ArrayList

In this article, we will discuss how to remove duplicate elements from ArrayList in Java

Already, we have discussed in one of the earlier article on How to remove duplicate elements from ArrayList maintaining insertion order

Remove duplicate elements from ArrayList :

  1. Using Collection class (i.e.; convert ArrayList to HashSet class to remove duplicates)
  2. Without using any Collection class (i.e.; using 2 for-loop iterations)

1. Using Collection class:

  • Iterate through original ArrayList to read duplicate elements
  • Create HashSet (using inter-conversion collection constructor)
  • Add ArrayList object to constructor argument of HashSet (to remove duplicates)
  • Reason: Set allows only unique elements
  • Again, iterate through HashSet to print unique elements

ArrayListToHashSet.java

package in.bench.resources.java.collections;

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

public class ArrayListToHashSet {

	public static void main(String[] args) {

		// creating ArrayList object of type String
		ArrayList<String> techCompanies = new ArrayList<String>();


		// adding elements to ArrayList object
		techCompanies.add("Sun");
		techCompanies.add("Apple");
		techCompanies.add("JBoss");
		techCompanies.add("Whatsup");
		techCompanies.add("Apple");
		techCompanies.add("BEA Weblogic");
		techCompanies.add("JBoss");


		// Iterating using enhanced for-loop
		System.out.println("Original ArrayList with duplicates:\n");
		for(String techCompany : techCompanies) {
			System.out.println(techCompany);
		}


		// remove duplicates,
		// by converting into HashSet's inter-conversion constructor


		// convert to HashSet
		Collection<String> hs = new HashSet<String>(techCompanies);


		// Iterating using enhanced for-loop
		System.out.println("\n\nUnique elements:\n");
		for(String techCompany : hs) {
			System.out.println(techCompany);
		}
	}
}

Output:

Original ArrayList with duplicates:

Sun
Apple
JBoss
Whatsup
Apple
BEA Weblogic
JBoss


Unique elements:

Apple
JBoss
Whatsup
BEA Weblogic
Sun

Note :

  • Above steps are very easy for development
  • but in coding interview they might ask about removing duplicate elements without using any Collection classes

2. Without using Collection class:

  1. Iterate through original ArrayList to read duplicate elements
  2. Construct outer for-loop for iterating ArrayList elements
  3. Construct inner for-loop for iterating ArrayList elements starting from 2nd position (or 1st index)
  4. Within inner for-loop, check whether outer for-loop element with inner for-loop elements (while iterating)
  5. Remove element from ArrayList, if it is found to be same; otherwise repeat step until both for-loop iteration gets completed
  6. Finally print ArrayList elements again using enhanced for-each loop

RemoveDuplicatesFromArrayList.java

package in.bench.resources.java.collections;

import java.util.ArrayList;

public class RemoveDuplicatesFromArrayList {

	public static void main(String[] args) {

		// creating ArrayList object of type String
		ArrayList<String> techCompanies = new ArrayList<String>();


		// adding elements to ArrayList object
		techCompanies.add("Sun");
		techCompanies.add("Apple");
		techCompanies.add("JBoss");
		techCompanies.add("Whatsup");
		techCompanies.add("Apple");
		techCompanies.add("BEA Weblogic");
		techCompanies.add("JBoss");


		// Iterating using enhanced for-loop
		System.out.println("Original ArrayList with duplicates:\n");
		for(String techCompany : techCompanies) {
			System.out.println(techCompany);
		}


		// remove duplicates, using 2 for-loops

		// outer for-loop
		for(int outForLoop = 0; outForLoop < techCompanies.size();
				outForLoop++) {

			// inner for-loop
			for(int inForLoop = outForLoop + 1;
					inForLoop < techCompanies.size(); inForLoop++) {

				// check whether, it already contains this element
				if(techCompanies.get(outForLoop).equals(
						techCompanies.get(inForLoop))) {

					// remove, if its already duplicate element
					techCompanies.remove(inForLoop);
				}
			}
		}


		// Iterating using enhanced for-loop
		System.out.println("\n\nUnique elements:\n");
		for(String techCompany : techCompanies) {
			System.out.println(techCompany);
		}
	}
}

Output:

Original ArrayList with duplicates:

Sun
Apple
JBoss
Whatsup
Apple
BEA Weblogic
JBoss


Unique elements:

Sun
Apple
JBoss
Whatsup
BEA Weblogic

Hope, you found this article very helpful. If you any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

References:

Happy Coding !!
Happy Learning !!

Java 8 - Various ways to remove duplicate elements from Arrays
Java - Iterate through Arrays in 6 ways