We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
functionmigratoryBirds(arr){// only birds type 1 - 5. create an array of zeros, and remember// arrays start at 0. so bird type 4 would be birdsCount[3].constbirdsCount=newArray(5).fill(0);// loop through arr and increment count based on arr[i] - 1.// why arr[i] - 1? again, because arrays start at 0.for(leti=0;i<arr.length;i++){birdsCount[arr[i]-1]+=1;}// now determine out of the birdsCount, which one has the largest count?constmaxCount=Math.max(...birdsCount);// using maxCount, which index in birdsCount appears first?// remember the index is bird type - 1. cause again, arrays start at 0.constfirstIndex=birdsCount.indexOf(maxCount);// using the index found, add 1.// why add 1? look at the first comment.returnfirstIndex+1;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Migratory Birds
You are viewing a single comment's thread. Return to all comments →
JavaScript solution: