arrays - Remove duplicate characters in a string in Java -
i started read famous "cracking coding interview" book.
design algorithm , write code remove duplicate characters in string without using additional buffer. note: 1 or 2 additional variables fine. copy of array not.
i found similar topic here : remove duplicate characters in string
the solution given author :
public static void removeduplicates(char[] str) { if (str == null) return; int len = str.length; if (len < 2) return; int tail = 1; (int = 1; < len; ++i) { int j; (j = 0; j < tail; ++j) { if (str[i] == str[j]) break; } if (j == tail) { str[tail] = str[i]; ++tail; } } str[tail] = 0; }
the problem here author used array argument function. question : how can write algorithms string argument? because felt it's easier use array here , it's "avoid difficulty" of exercice (in opinion, i'm newly java developer).
how can write such algorithm?
java strings immutable, can't string without copying array buffer.
Comments
Post a Comment