Migratory Birds (HackerRank - JavaScript Solution)

hokagedemehin

Ibukun Demehin

Posted on February 22, 2022

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.

Solution

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
💖 💪 🙅 🚩
hokagedemehin
Ibukun Demehin

Posted on February 22, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related