pre{ white-space: pre; } .entry-content pre{ word-wrap: normal; }

No.79 過小評価ダメ・ゼッタイ

最初はmapに突っ込んで、カウントの方針でAC通した。

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] l = new int[n];
		for(int i = 0 ; i < n ; i++) {
			l[i] = sc.nextInt();
		}
		Map<Integer, Integer> map = new HashMap<>();
		for(int i = 0 ; i < n ; i++) {
			map.put(l[i], 0);
		}
		for(int i = 0 ; i < n ; i++) {
			map.put(l[i], map.get(l[i]) + 1);
		}
		int max = -1001001001;
		int ans = 0;
		for(int key : map.keySet()) {
			if(max < map.get(key) || max == map.get(key) && ans < key) {
				ans = key;
				max = map.get(key);
			}
		}
		System.out.println(ans);
 	}
}

mapに突っ込まなくても、レベル:1 ~ 6の範囲で小さいから、配列に突っ込んでカウントしてもいいことも解答に乗ってたので、この方針でも解いてみた。

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] level = new int[7];
		for(int i = 0 ; i < n ; i++) {
			int temp = sc.nextInt();
			level[temp]++;
		}
		int ans = -1; int cnt = -1;
		for(int i = 1 ; i < 7 ; i++) {
			if(cnt <= level[i]) {
				cnt = level[i];
				ans = i;
			}
		}
		System.out.println(ans);
 	}
}