Skip to content

Instantly share code, notes, and snippets.

@harishkotra
Created July 10, 2018 12:43
Show Gist options
  • Save harishkotra/e84937bcdb22d1b35b6b989409bd9908 to your computer and use it in GitHub Desktop.
Save harishkotra/e84937bcdb22d1b35b6b989409bd9908 to your computer and use it in GitHub Desktop.
Design an algorithm and write code to remove the duplicate characters in a string
import java.io.*;
import java.util.*;
public class RemoveDuplicates{
public static void main(String []args){
String s = "aabnsbsbsssbbsbsbs";
char[] characters = s.toCharArray();
Set<Character> newSet = new LinkedHashSet<Character>();
for(char c: characters){
newSet.add(c);
}
StringBuilder sb = new StringBuilder();
for(Character charac: newSet){
sb.append(charac);
}
String newString = sb.toString();
System.out.println(newString);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment