DEV Community

Cover image for Migratory Birds (HackerRank - JavaScript Solution)
Ibukun Demehin
Ibukun Demehin

Posted on

Migratory Birds (HackerRank - JavaScript Solution)

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.


function migratoryBirds(arr) {
    // Write your code here

    let newArr = {}
    const uniqueVal = [... new Set(arr)]
    uniqueVal.forEach(elem => {
        const filterVal = arr.filter(val => val == elem)       
        const totalVal = filterVal.length
        newArr[elem] = totalVal

    let result = Object.entries(newArr).sort(([,a], [,b]) => b-a)
    let final = result[0][0]
    return final
Enter fullscreen mode Exit fullscreen mode

Top comments (0)