Sort by

recency

|

3638 Discussions

|

  • + 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;
    }
    
  • + 0 comments

    TypeScript:

    interface ITotals {
      [key: string]: number,
    }
    
    function migratoryBirds(arr: number[]): number {
      const totals: ITotals = arr.reduce<ITotals>((acc, e) => ({ ...acc, [e]: (acc[e] || 0) + 1}), {} as ITotals);
      const sorted = Object.keys(totals).sort((a, b) => totals[b] - totals[a]);
      
      return Number(sorted[0]);
    }
    
  • + 0 comments

    Java

    public static int migratoryBirds(List arr) {

    HashMap<Integer,Integer> map = new HashMap<>();
    for(int val:arr){
        if(map.containsKey(val)){
            map.put(val,map.get(val)+1);
        }
        else{
            map.put(val, 1);
        }
    }
    

    int max=0; for(int key:map.keySet()){ if(map.get(key)>max){ max=map.get(key); } } ArrayList list = new ArrayList<>(); for(int key:map.keySet()){ if(map.get(key)==max){ list.add(key); } } Collections.sort(list); return list.get(0); }