Sort by

recency

|

3636 Discussions

|

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

  • + 0 comments

    Ruby

    def migratoryBirds(arr)
      a = arr.tally
      a.max_by { |key, val| [val, -key] }.first
    end
    
  • + 0 comments

    can you help to integrate this algorithm script with my [portland flea for all ](https://antique-storesnear.me/portland-flea-for-all/)page. I want to use this on my site.