Answers for "Write a Java Program to remove all duplicate characters from the given string and return the resultant string."

2

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();
Posted by: Guest on May-22-2021

Code answers related to "Write a Java Program to remove all duplicate characters from the given string and return the resultant string."

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language