Skip to content

Instantly share code, notes, and snippets.

@thmain
Created October 15, 2017 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/c30f5cd51f0e2d1e094756b60d246ab4 to your computer and use it in GitHub Desktop.
Save thmain/c30f5cd51f0e2d1e094756b60d246ab4 to your computer and use it in GitHub Desktop.
import java.util.HashSet;
import java.util.Iterator;
public class RemoveDuplicatesUsingSet {
public static String removeDuplicates(String s){
HashSet<Character> set = new HashSet<Character>();
char [] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
set.add(chars[i]);
}
Iterator<Character> iterator = set.iterator();
String sbString = new String();
while (iterator.hasNext())
sbString += iterator.next()+"";
return sbString;
}
public static void main(String[] args) {
String s = "tutorialhorizon";
System.out.println(removeDuplicates(s));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment