Hackerrank - Migratory Birds Solution
2 min read

Hackerrank - Migratory Birds Solution

Hackerrank - Migratory Birds Solution

You have been asked to help study the population of birds migrating across the continent. Each type of bird you are interested in will be identified by an integer value. Each time a particular kind of bird is spotted, its id number will be added to your array of sightings. You would like to be able to find out which type of bird is most common given a list of sightings. Your task is to print the type number of that bird and if two or more types of birds are equally common, choose the type with the smallest ID number.

For example, assume your bird sightings are of types . There are two each of types  and , and one sighting of type . Pick the lower of the two types seen twice: type .

Function Description

Complete the migratoryBirds function in the editor below. It should return the lowest type number of the most frequently sighted bird.

migratoryBirds has the following parameter(s):

  • arr: an array of integers representing types of birds sighted

Input Format

The first line contains an integer denoting , the number of birds sighted and reported in the array .
The second line describes  as  space-separated integers representing the type numbers of each bird sighted.


  • It is guaranteed that each type is , , , , or .

Output Format

Print the type number of the most common bird; if two or more types of birds are equally common, choose the type with the smallest ID number.

Sample Input 0

1 4 4 4 5 3

Sample Output 0


Explanation 0

The different types of birds occur in the following frequencies:

  • Type : 1:1 bird
  • Type : 2:0 birds
  • Type : 3:1 bird
  • Type : 4:3 birds
  • Type : 5:1 bird

The type number that occurs at the highest frequency is type , so we print  as our answer.

Sample Input 1

1 2 3 4 5 4 3 2 1 3 4

Sample Output 1


Explanation 1

The different types of birds occur in the following frequencies:

  • Type : 1:2
  • Type : 2:2
  • Type : 3:3
  • Type : 4:3
  • Type : 5:1

Two types have a frequency of , and the lower of those is type .

Solution in python

def migratoryBirds(arr):
    count = [0]*6
    for i in arr:
        count[i] += 1
    return count.index(max(count))
arr = map(int,input().split())


First you initialize an array like this arr = [0,0,0,0,0,0] which can also be written as [0]*6
Now when you see bird 1 you will increment index 1 by 1, if you see bird 2 you will increment index 2 and so and so.

For example, you saw the following birds 2,2,2,3,3,5  then our array will become something like this


As you you may have noticed.
Index 1 increased by 0 since bird 1 is seen 0 time
Index 2 increased by 3 since bird 2 is seen 3 times
Index 3 increased by 2 since bird 3 is seen 2 times
Index 4 increased by 0 since bird 4 is seen 2 times
Index 5 increased by 1 since bird 5 is seen 1 time

Now max(arr) will give us 3, that is the count of the most seen bird.

arr.index(max(arr)) or arr.index(3) will give us 2 which means the bird in index 2 is seen the maximum number of times.

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.