Want to write articles/new answer to be published in javapedia.net? Send in your article with your name to javatutorials2016@gmail.com!

Prev Next

Java / Using String

How do I remove duplicate elements from a String Array in Java?

Using Java Collection framework API, the duplicate elements in a string array can be removed easily by converting the string array to List, creating a HashSet from the List that removes the duplicates and converting set to String array back. See the example below.

String[] data = { "a", "c", "b", "d","a", "c", "b", "d"};
System.out.println("Actual array: " + Arrays.toString(data));

List<String> strList = Arrays.asList(data);
Set<String> strSet = new HashSet<String>(strList);
String[] result = new String[strSet.size()];


System.out.println("Array without duplicates: ");
for (String s : result) {
  System.out.print(s + ", ");

The above program can be shortened further as shown below.

String[] data = { "a", "c", "b", "d", "a", "c", "b", "d" };

data = new HashSet<String>(Arrays.asList(data)).toArray(new String[0]);

for (String s : data) {
	System.out.print(s + ", ");

❤Cash Back At Stores you Love !!!❤

Earn your $10 reward when you make your first purchase through Ebates by signing up with clicking below button.

Ebates Coupons and Cash Back

More Related questions...

How do I iterate through every character in a String? (or) How to parse each character in a String object? toCharArray() Method How do I convert a char (primitive data type variable) to string in Java? Can we use String in switch case control statement? How do I split a pipe delimited Java String using split method? How do I create a Regular expression pattern into non-greedy in Java? Explain SimpleDateFormat class and its usage in Java. How to check if a string is not null and not empty in Java? How do I remove duplicate elements from a String Array in Java? How do I write a new line character to a file in Java? What is the maximum length of a String in Java? How many String objects be created by the snippet and where it is stored in Java? How do you convert a string object to character array in Java? Is string a wrapper class in Java? How many spaces does the Java String.trim() remove? Can we compare String and StringBuffer objects in Java? Which class is final:String, StringBuffer and StringBuider? What is the initial capacity of a StringBuilder object in Java? How to avoid NullPointerException when comparing two Strings in Java? Does substring method cause memory leak? what happens if beginIndex is equal to length of the String when calling substring(int beginIndex)? When substring method throws IndexOutOfBoundException? How to resolve substring memory leak issue in java version prior to JDK 1.7? Where String pool is located? What is the output of this statement?
System.out.println ("A"=="A");
What is the difference between appending a string to a StringBuilder and concatenating two strings with a + operator? How string concatenation operator (+) works in Java? When the concatenation of final string literal happen? is Java's SimpleDateFormat thread-safe?
Show more question and Answers...


Comments & Discussions