In this post, we will solve the Migratory Birds HackerRank Solution. This problem (Migratory Birds) is a part of the HackerRank Problem Solving series.
Problem:https://www.hackerrank.com/challenges/three-month-preparation-kit-migratory-birds/problem
Migratory Birds Hacker Rank Solution
Problem solution in Python programming:
def migratoryBirds(arr): # Write your code here arr = sorted(arr) freq = {} id = 0 counter = 0 for i in arr: if i in freq: freq[i] += 1 else: freq[i] = 1 if freq[i] > counter: id = i counter = freq[i] return id
Problem solution in JavaScript programming:
function migratoryBirds(arr) { if (arr.length == 0) {return 0} let count = new Array(5).fill(0); for (let i = 0; i < arr.length; i++) { count[arr[i]-1]++; } let max = Math.max(...count); return count.indexOf(max)+1; }
Problem solution in Java programming:
public static int migratoryBirds(List<Integer> arr) { // Write your code here int[] newInt = new int[6]; int value = 0; for(int i = 0; i < arr.size(); i++){ value = arr.get(i); newInt[value] += 1; } int mostFrequent = 0; for(int i = 1; i < newInt.length; i++){ if(newInt[mostFrequent] < newInt[i]){ mostFrequent = i; } } return mostFrequent; }
Problem solution in C++ programming:
int migratoryBirds(vector<int> arr) { map<int,int> m; int max[2]={0}; for(auto i : arr){ if(m.find(i)!=m.end()) { m[i]+=1; if(m[i]>max[1] || (m[i]==max[1] && i<max[0])) { max[0]=i; max[1]=m[i]; } } else m[i]=1; } return max[0]; }
Leave a Reply