Easiest way of checking if a string consists of unique characters?

Posted by serg555 on Stack Overflow See other posts from Stack Overflow or by serg555
Published on 2010-03-16T04:45:33Z Indexed on 2010/03/16 5:06 UTC
Read the original article Hit count: 317

Filed under:
|
|

I need to check in Java if a word consists of unique letters (case insensitive). As straight solution is boring, I came up with:

  1. For every char in a string check if indexOf(char) == lastIndexOf(char).
  2. Add all chars to HashSet and check if set size == string length.
  3. Convert a string to a char array, sort it alphabetically, loop through array elements and check if c[i] == c[i+1].

Currently I like #2 the most, seems like the easiest way. Any other interesting solutions?

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about string