Sort by

recency

|

3581 Discussions

|

  • + 0 comments

    python

    def migratoryBirds(arr):
        max_count = 0
        mostcommontype = -1
        for v in sorted(set(arr), reverse=True):
            count_v = arr.count(v)
            if count_v >= max_count:
                max_count = count_v
                mostcommontype = v
        return mostcommontype       
    
  • + 0 comments

    Python 3:

    def migratoryBirds(arr):
        arr_cnt = dict(Counter(arr))
        arr_dict = {i:v for i, v in sorted(arr_cnt.items(), key=lambda x: x[1])}
        arr_val = list(arr_dict.values())[-1]
        key = sorted([k for k, vv in arr_dict.items() if vv==arr_val])[0]
        return key
    
  • + 0 comments

    Here is my c++ solution you can find the explanation here : https://youtu.be/poQPrHTMe08

    int migratoryBirds(vector<int> arr) {
        map<int, int> mp;
        int cnt = 0, id;
        for(int i = 0; i < arr.size(); i++)mp[arr[i]]++;
        for(auto it = mp.begin(); it != mp.end(); it++){
            if(it->second > cnt || (it->second == cnt && it->first < id)){
                cnt = it->second;
                id = it->first;
            }
        }
        return id;
    }
    
  • + 0 comments
    public static int migratoryBirds(List<Integer> arr) {
        return arr.stream()
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
            .entrySet().stream()
        .sorted((a, b) -> (int) (b.getValue() - a.getValue()))
        .mapToInt(es -> es.getKey())
        .findFirst()
        .getAsInt();
    }
    
  • + 0 comments

    Simple solution

        public static int migratoryBirds(List<Integer> arr) {
            int[] freq = new int[5];
            int n = arr.size();
            int max = 0;
    
            for (int i: arr) {
                freq[i - 1]++;
                if (freq[i - 1] > max)
                max = freq[i - 1];
            }
            
            for (int i = 0; i < 5; i++) {
                if (freq[i] == max)
                return i + 1;
            }
            
            return -1;
        }