Skip to content

Instantly share code, notes, and snippets.

@yusufsyaifudin
Created May 1, 2015 15:57
Show Gist options
  • Save yusufsyaifudin/9bd867e413a2a531f53c to your computer and use it in GitHub Desktop.
Save yusufsyaifudin/9bd867e413a2a531f53c to your computer and use it in GitHub Desktop.
Return set of unique character from given string
String s = "hksaksaggah";//sample string
String temp2="";//string with no duplicates
HashMap<Integer, Character> tc = new HashMap<>();//create a hashmap to store the char's
char [] charArray = s.toCharArray();
for (Character c : charArray)//for each char
{
if (!tc.containsValue(c))//if the char is not already in the hashmap
{
temp2=temp2+c.toString();//add the char to the output string
tc.put(c.hashCode(),c);//and add the char to the hashmap
}
}
System.out.println(temp2);//final string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment