Sort by

recency

|

3628 Discussions

|

  • + 0 comments

    Java

    public static int migratoryBirds(List<Integer> arr) {
            int max = Integer.MIN_VALUE;
            int count = 0;
            int idMin = Integer.MAX_VALUE;
            int id = 0;
            
            Collections.sort(arr);
            
            for (int i = 0, j = 1; j < arr.size() - 1; i++, j++) {
                if (arr.get(i) == arr.get(j + 1)) {
                    id = arr.get(i);
                    count++;
                    if (count > max) {
                        max = count;
                        idMin = id;
                    }
                } else {
                    count = 0;
                }
            }
            return idMin;
        }
    
  • + 0 comments

    Fast and easy : Python3

    def migratoryBirds(arr):
        # Write your code here
        dic = {key:arr.count(key) for key in set(arr)}
        maxKey = [key for key,val in dic.items() if val == max(dic.values())]
        return min(maxKey)
    
  • + 0 comments

    My Python 3 O(n) solution, iterating once through array to build dictionary or hash map, then count the keys that have a value equal to maximum and append those keys to an array. If more than one key exists, the minimum does nothing:

    import os
    def migratoryBirds(arr):
        freq = {}
        for val in arr:
            freq[val] = freq.get(val, 0) + 1
        max_val = max(freq.values())
        keys = []
        for k, v in freq.items():
            if v == max_val:
                keys.append(k)
        return min(keys)
            
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        arr_count = int(input().strip())
        arr = list(map(int, input().rstrip().split()))
        result = migratoryBirds(arr)
        
        fptr.write(str(result) + '\n')
        fptr.close()
    
  • + 0 comments

    Here is my simple C++ solution,

    int migratoryBirds(vector arr) {

    int cnt = 0;
    int prevCnt = 0;
    int res  = 0;
    for( int i =1; i <=5; i++)  // if you read the constraint it says only 1 - 5 types guranteed, so we find the max of each element and the corresponding min
    {
        cnt = std::count (arr.begin(), arr.end(), i);
    
        if( prevCnt < cnt )
        {
            prevCnt = cnt;
            res = i;
        }
        else if( prevCnt == cnt )
        {
            res = min(res, i); // here we get the min of the max cnt if they are the same
        }
    }
    return res;   
    

    }

  • + 0 comments

    Here is my Python solution

    def migratoryBirds(arr):
        # Write your code here
        
        min_number = min(arr)
        max_number = max(arr)
    
        # Find the lowest type id of the most frequently sighted birds
        lowestId = max_number
        for i in range(max_number-1, min_number, -1):
            x = arr.count(i)
            if x >= (arr.count(lowestId)):
                lowestId = i
        return lowestId