我正在尝试从数组中删除重复项,但它不起作用。
我错过了什么吗?
代码 :-
class RemoveStringDuplicates {
public static char[] removeDups(char[] str) {
boolean bin_hash[] = new boolean[256];
int ip_ind = 0, res_ind = 0;
char temp;
while (ip_ind < str.length) {
temp = str[ip_ind];
if (bin_hash[temp] == false) {
bin_hash[temp] = true;
str[res_ind] = str[ip_ind];
res_ind++;
}
ip_ind++;
}
return str;
}
public static void main(String[] args) {
char str[] = "test string".toCharArray();
System.out.println(removeDups(str));
}
}
输出 :-
tes ringing //ing should not have been repeated!