You are viewing a single comment's thread. Return to all comments →
Java solution O(n) for any number of type of birds:
public static int migratoryBirds(List<Integer> arr) { Map<Integer, Integer> frequency = new HashMap<>(); int mostSeenBird = Integer.MAX_VALUE; for(int i = 0; i < arr.size(); i++){ frequency.put(arr.get(i), frequency.getOrDefault(arr.get(i), 0) + 1); int currentMostSeenFreq = frequency.getOrDefault(mostSeenBird,0); int currentKeyFreq = frequency.get(arr.get(i)); if(currentKeyFreq > currentMostSeenFreq || (currentKeyFreq == currentMostSeenFreq && mostSeenBird > arr.get(i))){ mostSeenBird = arr.get(i); } } return mostSeenBird; }
Seems like cookies are disabled on this browser, please enable them to open this website
Migratory Birds
You are viewing a single comment's thread. Return to all comments →
Java solution O(n) for any number of type of birds: