Review on code for duplicate removal I am trying to remove duplicates from a string without using additional buffers.
This seems to work correct, I think. Can this be improved? Should I be doing it differently?  

private static String removeDuplicates(char[] chars) {  

        if(chars == null) 
            return null;

        int len = chars.length;         
        for(int i = 0; i &lt; len; i++){
            for(int j = i + 1; j &lt; len;){
                if(chars[i] == chars[j]){
                    int temp = j;
                    while(temp &lt; len - 1){
                        chars[temp] = chars[temp + 1];
                        temp++;
                    }
                    len--;
                }
                else
                    j++;
            }
        }

        return new String(chars, 0, len);
    }
A few stylistic comments :


I'd change the while loop for a for loop. It doesn't matter at all but I feel it better that way.
You could perform len--; before the while (or for) loop so that you can go til len instead of len - 1.


Then, as duplicates don't need to be close to each other (from what I understand), wouldn't it be easier to do something like (NOT TESTED AT ALL) :

private static String removeDuplicates(char[] chars) {  
    if(chars == null) 
        return null;
    int len = chars.length;
    int idx = 0;         
    for (int i = 0; i &lt; len; i++) {
        char c = chars[i];
        bool dup = false;
        for (int j = 0; j &lt; idx; j++) {
            if (c == chars[j]) {
                dup = true;
                break;
            }
        }
        if (!dup) {
            chars[idx] = c;
            idx++;
        }
    }

    return new String(chars, 0, idx);
}


The point would be to go through the string and for each characters, try to see if we have seen it already. If we haven't we store it, otherwise, we don't.