In this HackerRank Migratory Birds problem, you have 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.
Problem solution in Python programming.
#!/bin/python3 import sys from collections import Counter import operator n = int(input().strip()) types = list(map(int, input().strip().split(' '))) # your code goes here mydict = dict(Counter(types)) maximum = max(mydict, key=mydict.get) print(maximum)
Problem solution in Java Programming.
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] types = new int[n]; for(int types_i=0; types_i < n; types_i++){ types[types_i] = in.nextInt(); } // your code goes here Map<Integer, Long> typesToCountMap = IntStream.of(types). boxed(). collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); Long maxCount = typesToCountMap.values().stream(). max(Comparator.naturalOrder()). get(); List<Integer> typeWithMaxCount = typesToCountMap.entrySet().stream(). filter(item -> Objects.equals(item.getValue(), maxCount)). map(Map.Entry::getKey). collect(Collectors.toList()); System.out.println(typeWithMaxCount.get(0)); } }
Problem solution in C++ programming.
#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; }
Problem solution in C programming.
#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, type, argmax = 0; int stats[5] = {0}; scanf("%d",&n); for(int types_i = 0; types_i < n; types_i++){ scanf("%d",&type); stats[type-1]++; } for(int i = 0; i < 5; i++){ if(stats[i] > stats[argmax]) argmax = i; } printf("%dn", argmax+1); return 0; }
Problem solution in JavaScript programming.
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(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function main() { var counts = {}; var max = 0; var maxKey; var n = parseInt(readLine()); types = readLine().split(' '); types = types.map(Number); // your code goes here for (var i = 0; i < types.length; i++) { if (counts[types[i]]) counts[types[i]] += 1; else counts[types[i]] = 1; } for (key in counts) { if (counts[key] > max) { max = counts[key]; maxKey = key; } } console.log(parseInt(maxKey)) }