I am working on an interview question where I need to remove duplicate characters from the String without using additional buffer.
Below is the code I have but it's not giving right output for this string "aabbab"
. Is there anything wrong in below code?
private static void removeDuplicates(char[] str) {
if (str == null)
return;
int len = str.length;
if (len < 2)
return;
int tail = 1;
for (int i = 1; i < len; ++i) {
int j;
for (j = 0; j < tail; ++j) {
if (str[i] == str[j])
break;
}
if (j == tail) {
str[tail] = str[i];
++tail;
}
}
str[tail] = 0;
}
I am not able to figure out what is wrong in the above code after debugging.