Java String array remove duplicates example shows how to remove duplicates from String array in Java. The example also shows how to remove duplicates from String array using Java 8 stream.
How to remove duplicates from the String array in Java?
There are several ways using which you can remove duplicates from the String array in Java as given below.
1) Java String array remove duplicates using Set (HashSet/LinkedHashSet)
One of the properties of the Set is that it does not allow duplicate elements. We can use this property to remove duplicates from an array as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.javacodeexamples.stringexamples; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashSet; public class RemoveDuplicatesFromStringArrayExample { public static void main(String[] args) { String[] strColors = { "red", "blue", "green", "red", //duplicate "yellow", "green" //duplicate }; System.out.println("Original array: " + Arrays.toString(strColors)); /* * convert array to list and then add all * elements to LinkedHashSet. LinkedHashSet * will automatically remove all duplicate elements. */ LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(Arrays.asList(strColors)); //create array from the LinkedHashSet String[] newArray = lhSetColors.toArray(new String[ lhSetColors.size() ]); System.out.println("Array after removing duplicates: " + Arrays.toString(newArray)); } } |
Output
1 2 |
Original array: [red, blue, green, red, yellow, green] Array after removing duplicates: [red, blue, green, yellow] |
We first converted an array to List using the asList
method of the Arrays class. Then we created a new LinkedHashSet object and added all elements of the List to it thus removing all the duplicate strings. Finally, we created a new array from the LinkedHashSet using the toArray
method. You can also use the HashSet class instead of the LinkedHashSet, but in that case order of the elements may not be maintained.
2) Using an ArrayList
We can also use the ArrayList class to remove the duplicates as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
String[] strColors = { "red", "blue", "green", "red", //duplicate "yellow", "green" //duplicate }; //create new ArrayList ArrayList<String> aListColors = new ArrayList<String>(); /* * Loop through array, add element to * ArrayList only if was not added previously */ for(int i=0; i < strColors.length; i++){ if( !aListColors.contains(strColors[i]) ){ aListColors.add(strColors[i]); } } //convert ArrayList back to array strColors = aListColors.toArray( new String[aListColors.size()] ); System.out.println("Array after removing duplicates: " + Arrays.toString(strColors)); |
Output
1 |
Array after removing duplicates: [red, blue, green, yellow] |
We first created an empty ArrayList object. Next, we looped through the array and checked if the array element already exists in the ArrayList using the contains
method. If it did not exist, we added the element to ArrayList thus skipping all duplicate elements. Finally, we converted the ArrayList to array using the toArray
method.
3) Using Java 8 Stream
If you are using Java 8, you can use the stream along with distinct to remove duplicates as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
String[] strColors = { "red", "blue", "green", "red", //duplicate "yellow", "green" //duplicate }; strColors = Arrays.stream(strColors).distinct().toArray(String[]::new); System.out.println("Array after removing duplicates: " + Arrays.toString(strColors)); |
Output
1 |
Array after removing duplicates: [red, blue, green, yellow] |
This example is a part of the Java String tutorial.
Please let me know your views in the comments section below.