Remove duplicate elements from ArrayList in Java

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 in Java

Solution:

  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:

Steps:

  1. Iterate through original ArrayList to read duplicate elements
  2. Create HashSet (using inter-conversion collection constructor)
  3. Add ArrayList object to constructor argument of HashSet (to remove duplicates)
  4. Reason: Set allows only unique elements
  5. 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

 

The 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:

Steps:

  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:

https://docs.oracle.com/javase/tutorial/collections/intro/
https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
https://docs.oracle.com/javase/tutorial/collections/implementations/list.html
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

 

Happy Coding !!
Happy Learning !!

Various ways to remove duplicate elements from Arrays in Java
Various ways to iterate Arrays in Java – 5 ways