感覺好像搞得太複雜(?
希望有人有更簡單更有效率的方法 可以分享
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
import java.util.ArrayList;
public class b374 {
public static void main (String[]args){
Scanner input = new Scanner(System.in);
while(input.hasNextInt()){
int n = input.nextInt();
HashMap<String,Integer> q = new HashMap<>();
int most = 0;
ArrayList<Integer> most2 = new ArrayList<>();
for (int i =0;i<n;i++) {
String a = input.next();
if (q.containsKey(a)) {
q.put(a, q.get(a) + 1);
} else {
q.put(a, 1);
}
if (q.get(a)==most){
most2.add(Integer.parseInt(a));
}
if (q.get(a)>most){
most =q.get(a);
most2.clear();
most2.add(Integer.parseInt(a));
}
}
Collections.sort(most2);
for (int i=0;i<most2.size();i++){
System.out.printf("%s %d\n",most2.get(i),q.get((String.valueOf(most2.get(i)))));
}
}
}
}