• + 0 comments
    class Pair{
            int val;
            int freq;
            Pair(int val,int freq){
                this.val=val;
                this.freq=freq;
            }
        }
    class Result {
    
        /*
         * Complete the 'migratoryBirds' function below.
         *
         * The function is expected to return an INTEGER.
         * The function accepts INTEGER_ARRAY arr as parameter.
         */
        
        public static int migratoryBirds(List<Integer> arr) {
        // Write your code here
            HashMap<Integer,Integer> mpp=new HashMap<>();
            for(int i=0;i<arr.size();i++){
                mpp.put(arr.get(i),mpp.getOrDefault(arr.get(i),0)+1);
            }
            int ans=Integer.MAX_VALUE;
            PriorityQueue<Pair> minheap=new PriorityQueue<>((a,b)-> a.freq==b.freq?a.val-b.val:b.freq-a.freq);
            for(Map.Entry<Integer,Integer> entry:mpp.entrySet()){
                minheap.add(new Pair(entry.getKey(),entry.getValue()));
            }
            return minheap.poll().val;
    
        }
    
    }