删除字符串中的重复字符

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);
    }

}
posted @ 2012-08-12 17:06  苦逼程序猴  阅读(189)  评论(0编辑  收藏  举报