# Migratory Birds – HackerRank Solution ## Problem :

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

### Input Format :

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

### Constraints :

• 5 <= n <= 2 * 1052
• It is guaranteed that each type is `1, 2, 3, 4`, or `5`.

### 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 :

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

### Sample Output 0 :

``4``

### Sample Input 1 :

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

### Sample Output 1 :

``3``

## Solution :

``````#include <bits/stdc++.h>
using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

// Complete the migratoryBirds function below.
int migratoryBirds(vector<int> arr) {
int count1, count2, count3, count4, count5;
for(int i = 0; i < arr.size(); i++) {
if (arr[i] == 1) {
count1++;
} else if (arr[i] == 2) {
count2++;
} else if (arr[i] == 3) {
count3++;
} else if (arr[i] == 4) {
count4++;
} else if (arr[i] == 5) {
count5++;
}
}
vector<int> count;
count.push_back(count1);
count.push_back(count2);
count.push_back(count3);
count.push_back(count4);
count.push_back(count5);

int sum = *max_element(count.begin(),count.end());
int size = 0;

for(int i = 0; i < 5; i++) {
if (sum == count[i]) {
size = i+1;
break;
}
}
return size;
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string arr_count_temp;
getline(cin, arr_count_temp);

int arr_count = stoi(ltrim(rtrim(arr_count_temp)));

string arr_temp_temp;
getline(cin, arr_temp_temp);

vector<string> arr_temp = split(rtrim(arr_temp_temp));

vector<int> arr(arr_count);

for (int i = 0; i < arr_count; i++) {
int arr_item = stoi(arr_temp[i]);

arr[i] = arr_item;
}

int result = migratoryBirds(arr);

fout << result << "\n";

fout.close();

return 0;
}

string ltrim(const string &str) {
string s(str);

s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);

return s;
}

string rtrim(const string &str) {
string s(str);

s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);

return s;
}

vector<string> split(const string &str) {
vector<string> tokens;

string::size_type start = 0;
string::size_type end = 0;

while ((end = str.find(" ", start)) != string::npos) {
tokens.push_back(str.substr(start, end - start));

start = end + 1;
}

tokens.push_back(str.substr(start));