Take the 2-minute tour ×

How to remove same words from string array? Here is code example i want to use, but it does not working

String[] wordList = outString.toString().split(", ");
for (int i = 0; i < wordList.length; i++) {
        if ((wordList[i].equals(wordList[j]))&&(j!=i)) {
share|improve this question
it does not working Well that's not helpful at all! What does it do? Remove all the words? Throw an exception? Not do anything? How can we read your mind to find out the actual problem? –  Doorknob Apr 9 '13 at 21:04
Suggest you sort the array, then for each word search forward until you find a non-matching word. –  Taylor Apr 9 '13 at 21:04
Do you mean removing duplicates? –  ssantos Apr 9 '13 at 21:05
This code won't even run... an Array doesn't have a remove method. Try again... –  Darwind Apr 9 '13 at 21:05
Set<String> set = new HashSet<String>(Arrays.asList(wordList)) –  vlcekmi3 Apr 9 '13 at 21:05
show 2 more comments

2 Answers

up vote 4 down vote accepted

A rule of Sets is that there can only be unique items in them. Therefore, the following code should suffice:

Set<String> mySet = new HashSet<String>(Arrays.asList(someArray));

share|improve this answer
add comment

You can use

Set<String> uniqueWords = new HashSet<>(Arrays.asList(outString.split(", ")));
share|improve this answer
add comment

Your Answer


