• + 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()