Last active
March 4, 2021 10:24
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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