您的位置:首页 > 其它

删除字符串中的重复字符

2012-10-28 21:46 381 查看
public class RemoveDuplicatedChar {

public static void removeDuplicated(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++;
}
}
while (tail < len) {
str[tail++] = '\0';
}
}

public static void main(String[] args) {
char[] str = { 'a', 'b', 'c', 'd', 'e', 'c', 'f', 'g', 'e', 'h' };
System.out.println(str);
removeDuplicated(str);
System.out.println(str);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: