Migratory Birds – HackerRank Problem 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 arr=[1,1,2,2,3] . There are two each of types 1  and 2, and one sighting of type 3. Pick the lower of the two types seen twice: type 1 .

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


1 4 4 4 5 3



Here is the original problem of HackerRank


Solution Logic:

Using .Net Linq Solution

  • Group by will provide key pair result in which we will count in value and key as unique number
  • Will sort the result to get min number out of it

Without Using Group by functionality

  • Create temp array with distinct item
  • Iterate through distinct item and get the count of each item
  • If count is higher than previous count changes the store the result and count
  • if count is equal to previous count then check set the lower number into result and perform the same action for all distinct item

Leave a Reply

Your email address will not be published. Required fields are marked *