0

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++) {
    for (int j = 0; j < wordList.length; j++) {
        if ((wordList[i].equals(wordList[j]))&&(j!=i)) {
            wordList.remove(wordList[i]);
        }
    }
}
Anton A.
  • 1,718
  • 15
  • 37

2 Answers2

4

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));

adchilds
  • 963
  • 2
  • 9
  • 22
2

You can use

Set<String> uniqueWords = new HashSet<>(Arrays.asList(outString.split(", ")));
System.out.println(uniqueWords);
jlordo
  • 37,490
  • 6
  • 58
  • 83