欢迎访问我的CCF认证解题目录
题目描述
思路过程
定义一个数组存储数字出现的次数,用for循环从小到大遍历,定义一个index、count存储当前的下标和次数,如果次数大于当前次数,则重新记录下标和次数
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] number = new int[10005];//存储每个数出现的次数
int index = 0;
int count = 0;
while ( n-- != 0 ) number[in.nextInt()]++;
for ( int i = 0; i < number.length; i++ ) {
if ( number[i] > count ) {//次数更大
index = i;
count = number[i];
}
}
System.out.println(index);
}
}