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 :
- Using Collection class (i.e.; convert ArrayList to HashSet class to remove duplicates)
- 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:
- Iterate through original ArrayList to read duplicate elements
- Construct outer for-loop for iterating ArrayList elements
- Construct inner for-loop for iterating ArrayList elements starting from 2nd position (or 1st index)
- Within inner for-loop, check whether outer for-loop element with inner for-loop elements (while iterating)
- Remove element from ArrayList, if it is found to be same; otherwise repeat step until both for-loop iteration gets completed
- 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 !!