Sort by

recency

|

3641 Discussions

|

  • + 0 comments

    C# solution:

    public static int migratoryBirds(List arr)

    {
        arr.Sort();
        var types = arr.Distinct().ToList();
        var counts = new List<int>();
        int count = 0;        
        foreach (int type in types)
        {
            for (int i = 0; i < arr.Count; i++)
                {
                    if(arr[i] == type)
                    {
                        count++;
                    }
                }
            counts.Add(count);
            count = 0; 
        }            
    
        int indexOfType = counts.IndexOf(counts.Max());
        return types[indexOfType];
    }
    
  • + 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())])