Sort by

recency

|

3640 Discussions

|

  • + 0 comments

    Here is how I solved it using C++. Feel free to leave feedback on my solution:

    `cpp

    int migratoryBirds(vector arr) {

    // Dump each bird type in a hashmap with its frequency
    std::unordered_map<int, int> map;
    for(int a : arr) {
        ++map[a];
    }
    
    // Then, check each type (start from type 5)
    // Return type with highest frequency (starting from end will result in highestCt having lowest ID)
    int resultType = 0;
    int highestCt = -1;
    for(int i = 5; i > 0; --i) {
        int count = map[i];
        if(count >= highestCt) {
            highestCt = count;
            resultType = i;
        }
    }
    
    return resultType;
    

    }`

  • + 0 comments

    Java Solution

    using HashMaps

     public static int migratoryBirds(List<Integer> arr) {
        // Write your code here
            int curMax = 0 ;
            HashMap<Integer , Integer> map = new HashMap<>();
            for(int num : arr){
                if(map.containsKey(num)){
                    int curFreq = map.get(num);
                    map.put(num , curFreq+1);
                    curMax = Math.max(curMax , map.get(num));
                }
                else{
                    map.put(num ,1);
                    curMax = Math.max(curMax,1);
                }
            }
            int res = Integer.MAX_VALUE;
            for(int key : map.keySet()){
                if(map.get(key) == curMax){
                    res = Math.min(res,key);
                }
            }
            return res;
        }
    
  • + 0 comments
    public static int migratoryBirds(List<Integer> arr) {
    
    Map<Integer,Integer> m=new HashMap<>();
    
        for(int i: arr){
            if(m.containsKey(i)){
                m.put(i, m.get(i)+1);
            }
            else{
                m.put(i, 1);
            }
        }
        int key=0;
        int value=0;
        for(int i:m.keySet()){
            if(m.get(i)>value){
                value=m.get(i);
                key=i;
            }
        }
        return key;
    
    }
    

    }

  • + 0 comments

    Clean problem — reminds me of how I categorized menu items by popularity on the Popeyes menu site. Here's my Python take using Counter:

    from collections import Counter
    
    def migratoryBirds(arr):
        counts = Counter(arr)
        return min([k for k, v in counts.items() if v == max(counts.values())])
    
  • + 0 comments
    int migratoryBirds(vector<int> arr)
    {
        int ret = 0;
        unordered_map<int, int> hMap;
        for(int i = 0; i < arr.size(); i++)
        {
            hMap[arr[i]]++;
        }
        int key1 = -1, maxOcc1 = -1;
        int key2 = -1, maxOcc2 = -1;
        for(auto it = hMap.begin(); it != hMap.end(); it++)
        {
            if(it->second > maxOcc1)
            {
                key1 = it->first;
                maxOcc1 = it->second;
                maxOcc2 = -1;
            }
            else if(it->second == maxOcc1)
            {
                key2 = it->first;
                maxOcc2 = it->second;
            }
        }
        if(maxOcc1 == maxOcc2)
        {
            if(key1 < key2) ret = key1;
            else ret = key2;
        }
        else
        {
            ret = key1;
        }
        return ret;
    }