Skip to content

Instantly share code, notes, and snippets.

@jishnujayakumar
Last active March 4, 2021 10:24
Show Gist options
  • Save jishnujayakumar/8e9a77fb8a5ae0b8afcbe494fea1dbe8 to your computer and use it in GitHub Desktop.
Save jishnujayakumar/8e9a77fb8a5ae0b8afcbe494fea1dbe8 to your computer and use it in GitHub Desktop.
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.
def migratory_birds(arr):
index = dict()
max_sight_count, min_ids = 0, 6
for bird_id in arr:
if bird_id not in index:
index[bird_id] = 0
index[bird_id] += 1
max_sight_count = max(index[bird_id], max_sight_count)
for bid, sight_count in index.items():
if sight_count == max_sight_count:
min_ids = min(min_ids, bid)
return min_ids
print(migratory_birds([1,2,1,5,4,4,4,5]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment