We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticintmigratoryBirds(List<Integer>arr){Map<Integer,Integer>map=newHashMap<>();intcount;// counter for number of sightingsintmax=0;// to track the highest number of sightingsintminID=Integer.MAX_VALUE;// to track the smallest IDfor(inti=0;i<arr.size();i++){count=map.getOrDefault(arr.get(i),0)+1;// get the current num of sightings for the ID and increment, defaults as 1 if the ID is not present in the map.map.put(arr.get(i),count);//update the number of sightings using ID as keyif(max<count){// if the highest num of sightings is less than the current num of sightings max=count;// update max num of sightings. minID=arr.get(i);// update the smallest ID}elseif(count==max&&arr.get(i)<minID){// of the current num of sightings == max numbers of sightings AND the current ID is less than the minimum IDminID=arr.get(i);// update the smallest ID only. }}returnminID;}
Cookie support is required to access HackerRank
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 →
Here is my O(n) solution using Java 8 HashMaps