I completed an exercise from the Cracking the Coding Interview:
Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not.
Follow-up: Write the test cases for this method.
I would like to ask whether my solution for the question is good enough, because the solution given in the book doesn't seem to work correctly.
import java.util.Arrays;
public class duplicateString{
public static void rduplication(char [] word){
if(word == null) return;
int len = word.length;
if(len<2) return;
int count=0;
for(int i =0;i<word.length;i++){
for(int j=0;j<word.length;j++){
if(word[i] == word[j]){
count = count + 1;
System.out.println(count);
}
if(word[i] == word[j] && count > 1){
word[j] =0;
}
}
count = 0;
}
System.out.println(Arrays.toString(word));
}
public static void main(String [] args){
String word = "ababab";
rduplication(word.toCharArray());
}
}