# HackerRank Migratory Birds Solution

#### ByBrokenprogrammers

Dec 12, 2022

Hello Programmers, In this post, you will know how to solve the HackerRank Migratory Birds Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

## HackerRank Migratory Birds Solution

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.

Example

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.

migratoryBirds has the following parameter(s):

• int arr[n]: the types of birds sighted

Returns

• int: the lowest type id of the most frequently sighted birds

Input Format

The first line contains an integer, n, the size of arr.
The second line describes arr as n space-separated integers, each a type number of the bird sighted.

Constraints

• 5 <= n <= 2 x 105
• It is guaranteed that each type is 1, 2, 3, 4, or 5.

Sample Input 0

```6
1 4 4 4 5 3
```

Sample Output 0

```4
```

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 4, so we print 4 as our answer.

Sample Input 1

```11
1 2 3 4 5 4 3 2 1 3 4
```

Sample Output 1

```3
```

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 3, and the lower of those is type 3.

## HackerRank Migratory Birds Solution

### Migratory Birds Solution in C

```#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int n;
int i;
int j;
int k;
int max;
int sth;
max = 0;
j = 1;
scanf("%d",&n);
sth = 0;
int *types = malloc(sizeof(int) * n);
for(int types_i = 0; types_i < n; types_i++){
scanf("%d",&types[types_i]);
}
// your code goes here
i = 0;
while (j <= 5 )
{
k = 0;
i = 0;
while (i < n)
{
if (types[i] == j)
k++;
i++;
}
if (k > max)
{
sth = j;
max = k;
}
j++;
}
printf("%d", sth);
return 0;
}```

### Migratory Birds Solution in Cpp

```#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5+10;
int N,A[10];
int main()
{
cin >> N;
for (int i=1,x; i <= N; i++) cin >> x, A[x]++;
int ans = 1;
for (int i=2; i <= 5; i++)
if (A[i] > A[ans]) ans = i;
cout << ans;
}```

### Migratory Birds Solution in Java

```import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] types = new int[n];
int[] count = new int[6];
int max=0;
for(int types_i=0; types_i < n; types_i++){
types[types_i] = in.nextInt();
count[types[types_i]]++;
if(count[types[types_i]]>max)
max=count[types[types_i]];
}
for(int i=0;i<count.length;i++)
{
if(max==count[i])
{
System.out.println(i);
break;
}

}

}
}```

### Migratory Birds Solution in Python

```#!/bin/python
import sys
n = int(raw_input().strip())
l =list( map(int, raw_input().strip().split(' ')))
l.sort()
ans=l[0]
count=1
max=1
for i in range(1,n):
if l[i]==l[i-1]:
count+=1
else:
count=1
if count>max:
max=count
ans=l[i]
print ans```

### Migratory Birds Solution using JavaScript

```process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var n = parseInt(readLine());
a = readLine().split(' ');
a = a.map(Number);
var max = 0;
b = new Array(6);
b.fill(0);
for(i=0;i<n;++i){
b[a[i]]++;

}
var ind = 0;
for(i=1;i<=5;++i){
if(b[i]>max){
max = b[i];
ind = i;
}
}
// your code goes here
console.log(ind);
}```

### Migratory Birds Solution in Scala

```object Solution {
def main(args: Array[String]) {
val sc = new java.util.Scanner (System.in);
var n = sc.nextInt();
//var types = new Array[Int](n);
var countArr = new Array[Int](5)
for(i <- 0 to n-1) {
var curr = sc.nextInt()-1
countArr(curr) = countArr(curr) + 1
}
var max = 0
for(i <- 1 to 4){
if(countArr(i) > countArr(max)){
max = i
}
}
println(max+1)
}
}```

### Migratory Birds Solution in Pascal

`will update sooon`

Disclaimer: This problem (Migratory Birds) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.