CCF 2013-12-1 出现次数最多的数

CCF 2013-12-1 出现次数最多的数_第1张图片

import java.util.Scanner;

/**
 * @author Shixiaodong
 * @date 2018/3/17 20:18
 */
public class Main {
    public static int[] array = new int[10001];
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        int temp = 0, time = -1, flag = -1;
        for (int i = 0; i < num; i++) {
            temp = scanner.nextInt();
            array[temp]++;
            if(array[temp] > time) {
                time = array[temp];
                flag = temp;
            } else if(array[temp] == time && temp <= flag) {
                time = array[temp];
                flag = temp;
            }
        }
        System.out.println(flag);
    }
}

你可能感兴趣的:(CCF)