Answers for "how to remove duplicate from a string in java"

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
1

java remove duplicates

public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list){
  Set<T> set = new LinkedHashSet<>(list);
  return new ArrayList<T>(set);
}
Posted by: Guest on June-16-2020

Code answers related to "how to remove duplicate from a string in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language