试题编号: | 201312-1 |
试题名称: | 出现次数最多的数 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 给定n个正整数,找出它们中出现次数最多的数。如果这样的数有多个,请输出其中最小的一个。 输入格式 输入的第一行只有一个正整数n(1 ≤ n ≤ 1000),表示数字的个数。 输出格式 输出这n个次数中出现次数最多的数。如果这样的数有多个,输出其中最小的一个。 样例输入 6 样例输出 10 |
import java.util.Scanner;
public class 出现次数最多的数201312_1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int len=sc.nextInt();
int[] a=new int[10001];
for(int i=1;i<=len;i++){
int num=sc.nextInt();
a[num]+=1;
}
// for(int i=1;i<=10000;i++){
// System.out.print(a[i]);
// }
int max=a[1];
int res=1;
int c;
for(c=1;c<=10000;c++){
if(a[c]>max){
max=a[c];//这个一定要加上的原因是 [6:7 7:7 8:7]应该返回6 如果只输出序号 则返回7了
res=c;
}
}
System.out.println(res);
}
}