问题描述
为库设计新函数DelPack,删除输入字符串中所有的重复元素。不连续的重复元素也要删除。
要求写成函数,函数内部使用指针操作。
样例输入
1223445667889
样例输出
13579
样例输入
else
样例输出
ls
数据规模和约定
字符串数组最大长度为100。
思路:考虑题中的数字和字母,记得转换就可。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string = scanner.next(); //定义字符串
char[] c = string.toCharArray(); //字符串转成char类型的字符数组
StringBuffer buffer=new StringBuffer(); //无参构造方法
int count=0; //统计
A: for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c.length; j++) {
if (c[i]==c[j]) {
count++;
}
}
if (count==1) {
buffer.append(c[i]); //复制到新数组中
}
count=0; //初始值
continue A; //重新进入循环
}
System.out.println(buffer.toString());
}