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.
classPair{intval;intfreq;Pair(intval,intfreq){this.val=val;this.freq=freq;}}classResult{/* * Complete the 'migratoryBirds' function below. * * The function is expected to return an INTEGER. * The function accepts INTEGER_ARRAY arr as parameter. */publicstaticintmigratoryBirds(List<Integer>arr){// Write your code hereHashMap<Integer,Integer>mpp=newHashMap<>();for(inti=0;i<arr.size();i++){mpp.put(arr.get(i),mpp.getOrDefault(arr.get(i),0)+1);}intans=Integer.MAX_VALUE;PriorityQueue<Pair>minheap=newPriorityQueue<>((a,b)->a.freq==b.freq?a.val-b.val:b.freq-a.freq);for(Map.Entry<Integer,Integer>entry:mpp.entrySet()){minheap.add(newPair(entry.getKey(),entry.getValue()));}returnminheap.poll().val;}}
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 →