Java面试题,输入一串大小写字母,找出出现次数第二多的字母
public class demo {
public static void main(String[] args) {
String scan=new Scanner(System.in).nextLine();
Map<Character,Integer> map = new HashMap();
for(int i=0;i<scan.length();i++){
char temp=scan.charAt(i);
if(map.containsKey(temp)){
map.put(temp, map.get(temp)+1);
}else{
map.put(temp, 1);
}
}
List<Map.Entry<Character,Integer>> list = new ArrayList<Map.Entry<Character,Integer>>(map.entrySet());
Collections.sort(list,new Comparator<Map.Entry<Character,Integer>>() {
@Override
public int compare(Map.Entry<Character,Integer> o1,
Map.Entry<Character,Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
System.out.println(list.get(1));
}
}