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 birdCounts = new HashMap(); for(int x = 0; x < types.length; x++){ Object typeCount = birdCounts.get(types[x]); if(typeCount == null){ //new type, set count to 1 birdCounts.put(types[x], 1); } else{ //update the count of birds of this type birdCounts.put(types[x], (int)typeCount+1); } } //sorted by value (smallest first) Map birdsTypeByCount = sortByValue(birdCounts); //get highest count of birds int highestCount = (int) birdsTypeByCount.values().toArray()[birdsTypeByCount.size()-1]; //get the keys containing the highest score Set birdsTypesByCount = getBirdsTypesByCount(birdsTypeByCount,highestCount); //because they were already sorted by count, the first item will be the smallest of those with //the highest count int birdTypeWithHighestCount = (int)birdsTypesByCount.toArray()[0]; System.out.println(birdTypeWithHighestCount); } public static Map sortByValue(Map unsortMap) { List> list=new LinkedList>(unsortMap.entrySet()); Collections.sort(list, new Comparator>() { public int compare(Map.Entry count2,Map.Entry count1) { return (count2.getValue()).compareTo(count1.getValue()); } }); //to order the insertions... Map sortedMap = new LinkedHashMap(); for (Map.Entry entry : list){ sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; } //Helper method to get key (bird type) by a value (number of birds of the type) public static Set getBirdsTypesByCount(Map map, Integer value) { Set keys = new HashSet(); for (Map.Entry entry : map.entrySet()) { if (Objects.equals(value, entry.getValue())) { keys.add(entry.getKey()); } } return keys; } }