Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java program to remove duplicate words in a string

String fullString = "lol lol";
String[] words = fullString.split("W+");
StringBuilder stringBuilder = new StringBuilder();
Set<String> wordsHashSet = new HashSet<>();

for (String word : words) {
    if (wordsHashSet.contains(word.toLowerCase())) continue;
    wordsHashSet.add(word.toLowerCase());
    stringBuilder.append(word).append(" ");
}
String nonDuplicateString = stringBuilder.toString().trim();
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #java #program #remove #duplicate #words #string
ADD COMMENT
Topic
Name
3+1 =