skip to Main Content

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:

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.

2

Answers


  1. 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++)
    
    Login or Signup to reply.
  2. A previous answer said you could use arr.reduce(a,b) => Math.max(a, b) to get the stopping index to use in your for-loop. That is an unnecessary work though, since that is an O(n) time complexity operation.

    Also there is no need to use Math.max, since you are told the array contains value 1-5. Therefore, using your algorithm, you can loop from 1 to 5. But your algorithm could still be improved a bit, since your algorithm runs O(k*n) time complexity (where k is the largest number in the array).

    You could trade the k time complexity for k space complexity if you use an array of size k to keep track of the count of each "bird". Simply loop through the input array, increasing the count of the index that matches the "bird"/number. Then find the index with the max value, return that index + 1 for the "bird" value.

    function migratoryBirds(arr) {
      const counts = [0, 0, 0, 0, 0]
      let max = 0;
      let maxBird = -1;
      arr.forEach(i => {
        let curr = Math.max(max, ++counts[i - 1]);
        if (curr > max) {
          maxBird = i;
          max = curr;
        }
      })
      return maxBird;
    }
    
    arr = [1, 1, 1, 1, 3, 2, 4, 1, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 2, 3, 2, 1]
    
    console.log(migratoryBirds(arr));

    Output = 5

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search