- Migratory Birds

 


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

There are two each of types  and , and one sighting of type . Pick the lower of the two types seen twice: type .

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, , the size of .
The second line describes  as  space-separated integers, each a type number of the bird sighted.

Constraints

  • It is guaranteed that each type is , or .

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  bird
  • Type  birds
  • Type  bird
  • Type  birds
  • Type  bird

The type number that occurs at the highest frequency is type , so we print  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 
  • Type 
  • Type 
  • Type 
  • Type 

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

Java 8:

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'migratoryBirds' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts INTEGER_ARRAY arr as parameter.
     */

    public static int migratoryBirds(List<Integer> arr) {
    // Write your code here
        List<Integer> traverse=new ArrayList<>();
        List<Integer> count_list=new ArrayList<>();
        Collections.sort(arr);
        
        for (int i=0;i<arr.size();i++){
            int item=arr.get(i);
            if (!traverse.contains(item)){
                count_list.add(Collections.frequency(arr,item));
                traverse.add(item);
            }   
        }
            
        int index_max_elem_occuar=count_list.indexOf(Collections.max(count_list));
        int result_elem=traverse.get(index_max_elem_occuar);
        return result_elem;

    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int arrCount = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$""").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        int result = Result.migratoryBirds(arr);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Python 3:

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'migratoryBirds' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER_ARRAY arr as parameter.
#

def migratoryBirds(arr):
    # Write your code here
    traverse=[]
    count_list=[]
    sorted(arr)
    
    for i in range(0,len(arr)):
        item=arr[i]
        if (item not in traverse):
            count_list.append(arr.count(item))
            traverse.append(item)
    index_max_elem_occuar=count_list.index(max(count_list))
    result_elem=traverse[index_max_elem_occuar]
    return result_elem
    
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

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

    arr = list(map(intinput().rstrip().split()))

    result = migratoryBirds(arr)

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

    fptr.close()

Compiler Message
Success
Input (stdin)
  • 6
  • 1 4 4 4 5 3
Expected Output
  • 4

Post a Comment

Previous Post Next Post