import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] types = new int[n]; for(int types_i=0; types_i < n; types_i++){ types[types_i] = in.nextInt(); } // your code goes here HashMap theBirds = new HashMap(); // Create frequency map for (int x : types) { Integer freq = theBirds.get(x); theBirds.put(x, (freq == null) ? 1 : ++freq); } //access the first element in the HashMap Iterator> iterator = theBirds.entrySet().iterator(); Map.Entry candidate = iterator.next(); Integer maxValue = candidate.getValue(); Integer maxKey = candidate.getKey(); for (Map.Entry entry : theBirds.entrySet()) { Integer currentKey = entry.getKey(); Integer currentValue = entry.getValue(); if (currentValue >= maxValue) { // Handle 1. case where frequency is equal, but lesser type # 2. case where freq strictly greater if ((currentValue.equals(maxValue) && currentKey < maxKey) || currentValue > maxValue) { maxKey = currentKey; maxValue = currentValue; } } } System.out.println(maxKey); } }