Skip to content

Instantly share code, notes, and snippets.

@silverjam
Created February 10, 2013 01:47
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 silverjam/4747958 to your computer and use it in GitHub Desktop.
Save silverjam/4747958 to your computer and use it in GitHub Desktop.
import java.util.*;
public class HasUniqueChars
{
public static boolean hasUniqueChars(String toTest)
{
char charArray[] = toTest.toCharArray();
Arrays.sort(charArray);
for (int x = 1; x < charArray.length; x++)
{
if ( charArray[x] == charArray[x-1] )
return false;
}
return true;
}
public static void main(String args[])
{
String testData[] = {
"abc",
"abcc",
"abcdefhi",
"abcdefhiabcdefhi",
"ccc",
"xxxkjsdfkjqqqdkjfd",
"abcdefghijklmnopqrstuvwxyz",
};
for (String test : testData)
{
if ( hasUniqueChars(test) )
{
System.out.println(" hasUniqueChars(\"" + test + "\")");
}
else
{
System.out.println("! hasUniqueChars(\"" + test + "\")");
}
}
}
}
// vim: sw=4:ts=4:sts=4:et:ai:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment