In this article, we will discuss various ways to remove duplicate elements from Arrays in Java i.e.;
Ways to remove duplicate elements from Arrays:
- Using List implemented classes
- Using Set implemented classes
- Using combination of both List & Set implemented classes
- Without using any collection classes
- Using Java 8 Streams
Let’s discuss one-by-one in detail with example/explanation
1. Using List implemented classes (i.e.; ArrayList class)
- Iterate through original Arrays to read duplicate elements
- Initialize ArrayList (i.e.; to store unique elements, after checking)
- While iterating String Array, check whether element already present in the unique list (created in step-2)
- If it doesn’t contains inside unique List, then add element to unique List
- Repeat step 3-4, until all elements of Arrays are compared and unique elements are stored inside List
- Convert unique list into Arrays using toArray() method
- Again, iterate through Arrays to print unique elements
RemoveDuplicateFromArraysUsingList.java
package in.bench.resources.java.arrays;
import java.util.ArrayList;
import java.util.List;
public class RemoveDuplicateFromArraysUsingList {
// main() method - entry point for JVM
public static void main(String[] args) {
// initialize an Arrays with duplicate values
String[] strArray = {
"Sun",
"Apple",
"JBoss",
"Whatsup",
"Apple", // duplicate
"BEA Weblogic",
"JBoss" // duplicate
};
// invoke removeDuplicatesFromArray() with above initialized Arrays
removeDuplicatesFromArray(strArray);
}
/**
* This method removes duplicate elements from Arrays
* using List class and finally prints unique elements
* @param strArray
*/
public static void removeDuplicatesFromArray(String[] strArray) {
// Iterating using enhanced for-loop
System.out.println("Original Arrays with duplicates:\n");
for(String str : strArray) {
System.out.println(str);
}
// initialize an empty ArrayList of String type
List<String> uniqueList = new ArrayList<String>();
// remove duplicate iterating through Arrays
for(int index = 0; index < strArray.length; index++) {
// check whether list contains duplicate, while iterating
if(!uniqueList.contains(strArray[index])) {
// if it is doesn't contains, then add to unique list
uniqueList.add(strArray[index]);
}
}
// convert unique List into Array using toArray() method
strArray = uniqueList.toArray(new String[0]);
// Iterating using enhanced for-loop
System.out.println("\n\nUnique elements:\n");
for(String str : strArray) {
System.out.println(str);
}
}
}
Output:
Original Arrays with duplicates:
Sun
Apple
JBoss
Whatsup
Apple
BEA Weblogic
JBoss
Unique elements:
Sun
Apple
JBoss
Whatsup
BEA Weblogic
2. Using Set implemented classes (i.e.; HashSet class)
- Iterate through original Arrays to read duplicate elements
- Initialize HashSet (i.e.; to store unique elements, while iterating)
- While iterating String Array, just simply add elements to HashSet; because Set allows only unique items thereby removing duplicate items
- Convert Set into Arrays using toArray() method
- Again, iterate through Arrays to print unique elements
RemoveDuplicateFromArraysUsingSet.java
package in.bench.resources.java.arrays;
import java.util.HashSet;
import java.util.Set;
public class RemoveDuplicateFromArraysUsingSet {
// main() method - entry point for JVM
public static void main(String[] args) {
// initialize an Arrays with duplicate values
String[] strArray = {
"Sun",
"Apple",
"JBoss",
"Whatsup",
"Apple", // duplicate
"BEA Weblogic",
"JBoss" // duplicate
};
// invoke removeDuplicatesFromArray() with above initialized Arrays
removeDuplicatesFromArray(strArray);
}
/**
* This method removes duplicate elements from Arrays
* using Set class and finally prints unique elements
* @param strArray
*/
public static void removeDuplicatesFromArray(String[] strArray) {
// Iterating using enhanced for-loop
System.out.println("Original Arrays with duplicates:\n");
for(String str : strArray) {
System.out.println(str);
}
// initialize an empty HashSet of String type
Set<String> set = new HashSet<String>();
// iterate through Arrays to remove duplicates
for(int index = 0; index < strArray.length; index++) {
// add elements to HashSet, which doesn't allow duplicates
set.add(strArray[index]);
}
// convert unique Set into Arrays using toArray() method
strArray = set.toArray(new String[0]);
// Iterating using enhanced for-loop
System.out.println("\n\nUnique elements:\n");
for(String str : strArray) {
System.out.println(str);
}
}
}
Output:
Original Arrays with duplicates:
Sun
Apple
JBoss
Whatsup
Apple
BEA Weblogic
JBoss
Unique elements:
Apple
JBoss
Whatsup
BEA Weblogic
Sun
Difference between above 2 implemented approaches:
- In the 1st way, we have to check manually for each items inside String Arrays with unique List and then add to List for each iteration
- Whereas for Set implemented classes, we don’t need to do any comparison or checking because Set allows only unique items thereby removing duplicate items
- In addition to this, 1st approach maintains insertion order whereas 2nd approach follows random order as HashSet internally uses hashing algorithm to store elements
3. Using both List & Set classes
- Iterate through original Arrays to read duplicate elements
- Convert Arrays into List; using Arrays’ asList(arrObj); method
- Add converted List object into HashSet using inter-conversion collection constructor; for removing duplicates
- Create new Arrays of required data-type
- Convert Set to Arrays (using newly created Arrays in step-4)
- Again, iterate through HashSet to print unique elements
RemoveDuplicateUsingListAndSet.java
package in.bench.resources.java.arrays;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class RemoveDuplicateUsingListAndSet {
// main() method - entry point for JVM
public static void main(String[] args) {
// initialize an Arrays with duplicate values
String[] strArray = {
"Sun",
"Apple",
"JBoss",
"Whatsup",
"Apple", // duplicate
"BEA Weblogic",
"JBoss" // duplicate
};
// invoke removeDuplicatesFromArray() with above initialized Arrays
removeDuplicatesFromArray(strArray);
}
/**
* This method removes duplicate elements from Arrays
* using List and Set classes and finally prints unique elements
* @param strArray
*/
public static void removeDuplicatesFromArray(String[] strArray) {
// Iterating using enhanced for-loop
System.out.println("Original Arrays with duplicates:\n");
for(String str : strArray) {
System.out.println(str);
}
// convert Arrays into List
List<String> lst = Arrays.asList(strArray);
// again convert List into Set, for removing duplicates
// using inter-conversion constructor
Set<String> set = new HashSet<String>(lst);
// create new String[] with no. of elements inside Set
String[] uniqueArr = new String[set.size()];
// convert back Set into Arrays
set.toArray(uniqueArr);
// Iterating using enhanced for-loop
System.out.println("\n\nUnique elements:\n");
for(String uStr : uniqueArr) {
System.out.println(uStr);
}
}
}
Output:
Original Arrays with duplicates:
Sun
Apple
JBoss
Whatsup
Apple
BEA Weblogic
JBoss
Unique elements:
Apple
JBoss
Whatsup
BEA Weblogic
Sun
Note : above discussed ways are very easy for development, but in coding interview they might ask about removing duplicates elements without using any Collection classes
4. Without using any collection classes
- Iterate through original Arrays to read duplicate elements
- Construct outer for-loop for iterating Arrays elements
- Construct inner for-loop for iterating Arrays 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)
- If comparison comes out to be true, then assign last element at this position (thereby replacing duplicate element)
- While doing step 5, decrement the size of Arrays by 1 as well as decrement inner for-loop count (this will give unique elements inside Arrays after all iterations are completed)
- Finally print Arrays elements again
RemoveDuplicateOfArrays.java
package in.bench.resources.java.arrays;
import java.util.Arrays;
public class RemoveDuplicateOfArrays {
// main() method - entry point for JVM
public static void main(String[] args) {
// initialize an Arrays with duplicate values
String[] strArray = {
"Sun",
"Apple",
"JBoss",
"Whatsup",
"Apple", // duplicate
"BEA Weblogic",
"JBoss" // duplicate
};
// invoke removeDuplicatesFromArray() with above initialized Arrays
removeDuplicatesFromArray(strArray);
}
/**
* This method removes duplicate elements from Arrays
* using 2 for-loops and finally prints unique elements
* @param strArray
*/
public static void removeDuplicatesFromArray(String[] strArray) {
// Iterating using enhanced for-loop
System.out.println("Original Arrays with duplicates:\n");
for(String str : strArray) {
System.out.println(str);
}
// get size of the Arrays using length property
int sizeOfArrays = strArray.length;
// remove duplicates, using 2 for-loops
// outer for-loop
for(int outForLoop = 0; outForLoop < sizeOfArrays; outForLoop++) {
// inner for-loop
for(int inForLoop = outForLoop + 1;
inForLoop < sizeOfArrays; inForLoop++) {
// check whether, it already contains this element
if(strArray[outForLoop] == strArray[inForLoop]){
// if elements are same, then replace with last item
strArray[inForLoop] = strArray[sizeOfArrays-1];
// at the same time, decrement size value by 1
sizeOfArrays--;
// also decrement inner for-loop
inForLoop--;
}
}
}
// create new String[] to copy unique elements
String[] uniqueStrArrays = Arrays.copyOf(strArray, sizeOfArrays);
// Iterating using enhanced for-loop
System.out.println("\n\nUnique elements:\n");
for(String str : uniqueStrArrays) {
System.out.println(str);
}
}
}
Output:
Original Arrays with duplicates:
Sun
Apple
JBoss
Whatsup
Apple
BEA Weblogic
JBoss
Unique elements:
Sun
Apple
JBoss
Whatsup
BEA Weblogic
5. Using Java 8 Streams
- Iterate through original Arrays to read duplicate elements
- Using Streams class, remove duplicate elements (i.e.; using distinct() method)
- At the same time, convert back to Object[] arrays by invoking toArray() method
- Finally print Arrays elements again
RemoveDuplicateUsingStreamsInJava8.java
package in.bench.resources.java.arrays;
import java.util.Arrays;
public class RemoveDuplicateUsingStreamsInJava8 {
// main() method - entry point for JVM
public static void main(String[] args) {
// initialize an Arrays with duplicate values
String[] strArray = {
"Sun",
"Apple",
"JBoss",
"Whatsup",
"Apple", // duplicate
"BEA Weblogic",
"JBoss" // duplicate
};
// invoke removeDuplicatesFromArray() with above initialized Arrays
removeDuplicatesFromArray(strArray);
}
/**
* This method removes duplicate elements from Arrays
* using Streams class in Java 8 and finally prints unique elements
* @param strArray
*/
public static void removeDuplicatesFromArray(String[] strArray) {
// Iterating original Arrays using enhanced for-loop
System.out.println("Original Arrays with duplicates:\n");
for(String str : strArray) {
System.out.println(str);
}
// convert to unique/distinct Arrays using Java 8 Streams class
Object[] uniqueArrays = Arrays
.stream(strArray)
.distinct()
.toArray();
// Iterating unique Arrays using enhanced for-loop
System.out.println("\n\nUnique elements:\n");
for(Object obj : uniqueArrays) {
System.out.println(obj.toString());
}
}
}
Output:
Original Arrays with duplicates:
Sun
Apple
JBoss
Whatsup
Apple
BEA Weblogic
JBoss
Unique elements:
Sun
Apple
JBoss
Whatsup
BEA Weblogic
Related Articles:
- Java 8 – How to find duplicate and its count in a Stream or List ?
- Java 8 – How to remove duplicates from ArrayList
- Java 8 – How to remove duplicates from LinkedList
- Java 8 – How to find duplicate and its count in an Arrays ?
- Java 8 – How to remove duplicate from Arrays ?
- Java 8 – Various ways to remove duplicate elements from Arrays
- Java 8 – How to remove an entry from HashMap by comparing keys
- Java 8 – How to remove an entry from HashMap by comparing values
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/7/docs/api/java/util/Arrays.html
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Array.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.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
- https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/class-use/HashSet.html
- https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html
- https://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashSet.html
- http://docs.oracle.com/javase/7/docs/api/index.html?java/util/class-use/LinkedHashSet.html
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.
Happy Coding !!
Happy Learning !!