Migratory Birds

November 09, 2017


A flock of birds is flying across the continent. Each bird has a type, and the different types are designated by the ID numbers 1, 2, 3, 4, and 5.

Given an array of integers where each integer describes the type of a bird in the flock, find and 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.

Input Format

The first line contains an integer denoting (the number of birds). The second line contains space-separated integers describing the respective type numbers of each bird in the flock.

Constraints

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

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import sys

def migratoryBirds(n, ar):
    # Complete this function
    most = 0
    mostID = 0
    
    IDs = [0,0,0,0,0]
    for i in range(n):
        IDs[ar[i]-1] += 1
    
    for i in range(5):
        if most < IDs[i]: 
            most = IDs[i]
            mostID = i + 1
      
    return mostID

n = int(input().strip())
ar = list(map(int, input().strip().split(' ')))
result = migratoryBirds(n, ar)
print(result)