Check if a String has all unique characters

Just a random algorithm post

public static boolean checkStringUniqueCharacters(String str){
   HashSet hs = new HashSet();
   char[] chars = str.toCharArray();

   for(int i = 0; i < chars.length; i++){
      if(hs.contains(chars[i])){
         return false;
      } else {
         hs.add(chars[i]);
      }
   }
   return true;
}

Lulz

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s