题目
要求统计一个整型序列中出现次数最多的整数及其出现次数。
输入格式:
在一行中给出序列中整数个数N(0<N≤1000),依次给出N个整数,每个整数占一行。
输出格式:
在一行中输出出现次数最多的整数及其出现次数,数字间以空格分隔。题目保证这样的数字是唯一的。
输入样例:
在这里给出一组输入。例如:
10
3
2
-1
5
3
4
3
0
3
2
输出样例:
在这里给出相应的输出。例如:
3 4
代码
import java.util.*;
public class Main{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int m=in.nextInt();
Map<Integer,Integer> p=new HashMap<>();
while (m>0){
int key=in.nextInt();
if (p.containsKey(key)) p.put(key,p.get(key)+1);
else p.put(key,1);
m--;
}
int max=-1;
for (Integer i:p.values()){
if (i>max) max=i;
}
for (Integer i:p.keySet()){
if (p.get(i)==max) System.out.print(i+" "+max);
}
}
}
笔记
- 掌握map结构即可