# Migratory Birds | HackerRank Solution

Hello coders, today we are going to solve Migratory Birds HackerRank Solution which is a Part of HackerRank Algorithm Series.

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 1234, 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 11 bird
• Type 20 birds
• Type 31 bird
• Type 43 birds
• Type 51 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 12
• Type 22
• Type 33
• Type 43
• Type 51

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

## Solution – Migratory Birds

### C++

```#include <bits/stdc++.h>

using namespace std;

const int maxN = 1e5+10;
int N,A;

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;
}```

### Python

```#!/bin/python3

import math
import os
import random
import re
import sys

def migratoryBirds(arr):
count =  * 6
for i in arr:
count[i] += 1
return count.index(max(count))

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

arr_count = int(input().strip())

arr = list(map(int, input().rstrip().split()))

result = migratoryBirds(arr)

fptr.write(str(result) + '\n')

fptr.close()
```

### 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[] birds = new int;
for (int i = 0; i < n; i++) birds[in.nextInt()-1]++;
int max = 0;
int id = 0;
for (int i = 0; i < 5; i++) {
if (birds[i] > max) {
max = birds[i];
id = i+1;
}
}
System.out.println(id);
}
}```

Disclaimer: The above Problem (Migratory Birds) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.