Skip to content

Instantly share code, notes, and snippets.

@orhanobut
Last active December 23, 2015 12:49
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 orhanobut/6637791 to your computer and use it in GitHub Desktop.
Save orhanobut/6637791 to your computer and use it in GitHub Desktop.
Remove duplicate chars from a string for english word. Complexity = O (n)
public static String removeDuplicateChars2(String s)
{
if (s == null) {
return s;
}
int len = s.length();
if (len < 2){
return s;
}
char[] c = s.toCharArray();
boolean[]hit = new boolean[256];
for (int i = 0; i < hit.length; i++) {
hit[i] = false;
}
hit[c[0]] = true;
int k = 1;
for (int i = 1; i < len; i++) {
if (!hit[c[i]]) {
hit[c[i]] = true;
c[k] = c[i];
k++;
}
}
return new String(c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment