I am trying to solve one of HackerRanks easy rated problems with Python. My code solves 4 out of 5 test cases but for one of the test cases, I get an error message saying time limit exceeded. Please recommend ways to optimize this. I am a beginner at python and still trying to understand itertools but it seems quite complicated.
Problem Statement: Given an array of bird sightings where every element represents a bird type id, determine the id of the most frequently sighted type. If more than 1 type has been spotted that maximum amount, return the smallest of their ids.
Sample input: arr=[1,2,3,4,5,4,3,2,1,3,4] Sample output: 3
My current code:
def migratoryBirds(arr):
d={}
arr2=[]
for i in range(len(arr)):
d[arr[i]]=arr.count(arr[i])
for i in d.keys():
if d[i]==max(d.values()):
arr2.append(i)
return(min(arr2))