来个效率比较低的,不知道有没有bug
public class Main {
public static void main(final String[] args) {
char[] source = { 'a', 'a', 'b', 'b', 'b', 'b', 'a', 'c', 'd', 'd', 'a
' };
new Main().removeDup(source);
for (char c : source) {
if (c != 0) {
System.out.print(c);
} else {
break;
}
}
System.out.println();
}
void removeDup(final char[] source) {
for (int i = 1; i < source.length; i++) {
if (this.contains(source[i], source, i - 1)) {
source[i] = 0;
}
}
this.compact(source);
}
boolean contains(final char c, final char[] source, final int k) {
for (int i = 0; i <= k; i++) {
if (source[i] == c) {
return true;
}
}
return false;
}
void compact(final char[] source) {
for (int i = 1; i < source.length; i++) {
if (source[i] != 0) {
for (int j = 0; j < i; j++) {
if (source[j] == 0) {
source[j] = source[i];
source[i] = 0;
break;
}
}
}
}
}
}