I want to remove/ delete duplicate words. I tried this but it wont work. Does someone have a solution for this?
public static void main(String[] args) {
List<String> words = sw("This is is an example");
System.out.println(words); // => [an, example, is, This]
}
public static List<String> sw(String s) {
List<String> words = new ArrayList<String>(Arrays.asList(s.trim().split(" +")));
int size = words.size();
for(int i = 0;i < size;i++) {
for(int k = i+1;k < size;k++) {
if(size == 1) {
break;
}
if(k < size -1 && words.get(i).equals(words.get(k))) {
words.remove(k);
k = k -1;
} if(size == 1) {
break;
}
}
}
Collections.sort(words);
return words;
}
}