算法训练 出现次数最多的整数

Problem:

算法训练 出现次数最多的整数

Hint:

  1.  这里唯一的坑点就是要对N进行判断是否大于0........被卡在80分好久

AcCode:

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

public class Main{
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		if(N>0) {
			Map<Long, Integer> map = new HashMap<Long, Integer>();
			long maxNum = -1;
			int maxCount = -1;
			boolean isFist = true;
			while(N!=0) {
				long key = in.nextLong();
				if(!map.containsKey(key)) {
					map.put(key, 1);
					if(isFist) {
						maxNum = key;
						maxCount = 1;
						isFist = false;
					}
				}else {
					int tempNum = map.get(key);
					tempNum++;
					if(tempNum>maxCount) {
						maxNum = key;
						maxCount = tempNum;
					}
					map.put(key, tempNum);
				}
				N--;
			}
			System.out.println(maxNum);
		}
	}
}