Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Correct output but told I'm wrong

I’m a beginner in Javascript, and currently I’m doing some practice on HackerRank. I’m doing the "Migratory Birds" assignment, and 1 of the test cases is failing.

The assignment gives an array, and each element contains the type of bird spotted. You are supposed to return the most common bird spotted.

This is my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function migratoryBirds(arr) {
    let highestCount = 0
    let mostCommonType = 0
    for(let i=1; i<=Math.max(...arr); i++) {
        let count = arr.filter(element => element == i).length
        if(count>highestCount) {
            highestCount = count
            mostCommonType = i
        } 
    }
    return mostCommonType
}

The test case for which I have an issue contains the following:

124992 (Array length)
5 2 2 2 4 1 1 2 4 2 2 2 4 1 2 4 1 2 4 4 3 2 3 1 3 3 4 3 5 2 5 3 4 1 3 2 3 3 3 5 2 4 1 5 4 5 4 4 4 5 3 2 1 1 3 1 1 5 5 3 5 2 2 4 5 2 4 3 2 4 4 5 3 2 3 2 4 5 2 2 3 5 2 3 1 3 3 2 4 3 5 4 3 1 3 3 2 4 4 3 5 3 3 3 5 1 3 5 5 2 5 2 3 4 3 3 2 1 3 1 2 3 2 4 2 3 3 3 3 4 3 3 1 1 5 1 3 4 5 5 3 3 1 5 5 5 5 2 3 1 3 2 3 5 5 1 1 3 4 1 1 2 4 4 4 1 2 3 3 2 1 5 3 1 1 2 2 1 5 2 1 1 4 2 4 5 2 2 2 1 1 1 3 2 4 5 1 4 4 1 5 2 1 4 3 5 4 2 1 5 5 5 2 1 4 5 2 2 1 2 4 3 2 4 3 3 5 3 5 1 4 1 2 4 2 1 5 5 1 1 5 5 1 3 5 2 5 4 1 1 2 1 5 2 3 3 1 1 2 2 5 2 1 3 5 5 4 2 5 5 4 2 1 3 3 1 2 5 5 1 4 4 5 4 3 2 4 5 1 4 1 2 2 4 5 3 3 5 1 4 2 5 1 5 3 3 2 4 3 5 1 2 4 2 3 4 4 4 4 3 4 5 1 2 3 1 5 2 2 3 5 4 5 3 2 3 3 3 1 4 2 3 3 4 4 3 2 2 2 2 1 4 2 3 1 4 4 5 4 1 3 1 2 3 4 3 2 2 3 2 3 5 2 3 3 1 1 3 4 1 2 3 3 4 5 3 2 4 2 2 3 1 3 1 3 1 2 1 1 4 3 3 1 3 4 1 4 4 5 5 2 5 4 2 5 4 1 3 1 2 2 5 4 4 2 2 5 4 2 3 5 5 1 3 1 2 1 2 1 2 5 4 5 4 3 5 1 4 5 1 5 5 2 3 2 3 5 1 1 4 4 5 5 5 4 5 2 4 2 3 3 2 4 2 5 2 3 3 2 4 3 5 3 4 5 5 2 1 4 5 2 1 2 5 1 1 3 3 5 5 4 2 4 3 1 3 1 4 3{-truncated-}

The program expects a 3 as an answer but when I run this as a custom input, I also get a 3. I am not sure what I did wrong, since the other test cases work fine.

>Solution :

The array in one test case is too large for the stack, so using spread syntax with Math.max produces an error. Since the problem specifies that the types range from 1 to 5, you can simply loop over that interval instead.

for (let i = 1; i <= 5; i++)

Alternatively, you could use Array#reduce to get the maximum instead.

for (let i = 1; i <= arr.reduce((a, b) => Math.max(a, b)); i++)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading