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/6637780 to your computer and use it in GitHub Desktop.
Save orhanobut/6637780 to your computer and use it in GitHub Desktop.
Remove duplicate char from a string value. Complexity = O (n^2)
public static String removeDuplicateChars(String s) {
if (s == null)
return s;
int len = s.length();
if (len < 2) {
return s;
}
char[] chars = s.toCharArray();
int index = 0;
for (int i = 0; i < len; i++) {
int j;
for (j = 0; j < index; j++) {
if (chars[j] == chars[i]) {
break;
}
}
if (j == index) {
char c = chars[i];
chars[i] = ' ';
chars[index] = c;
index++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment