之前面试遇到的一个面试题,分享一下我的答案
public static void main(String[] args) throws CloneNotSupportedException {
String data="aabbccdxdeff";
List<String> list=new ArrayList<>();
char[] chars = data.toCharArray();
Map<String,Integer>map=new HashMap<>();
for (int i = 0; i < chars.length; i++) {
list.add(String.valueOf(chars[i]));
}
for (int i=0;i<list.size();i++){
for (int j=0;j<list.size();j++){
if (list.get(i).equals(list.get(j))){
System.out.println(list.get(i));
System.out.println(list.get(j));
if (null!=map.get(list.get(i))){
map.put(list.get(i),map.get(list.get(i))+1);
System.out.println(list.get(i)+"和"+list.get(j)+"相同"+map.get(list.get(i))+1+"次");
}
else {
System.out.println(list.get(i)+"和"+list.get(j)+"相同一次");
map.put(list.get(i),1);
}
}
}
if (map.get(list.get(i)).equals(1)){
System.out.println("第一个不重复数为"+list.get(i));
break;
}
}
}
|