skip to Main Content
function findLargestNumbers(arrayOfArrays) {
    let k = new Array;
    for(let i = 0 ; i < arrayOfArrays.length ; i++){
        if(arrayOfArrays[i].length == 0){ 
            continue ;
        }
        let ans = arrayOfArrays[i][0];
        for(let j = 0 ; j < arrayOfArrays[i].length ; j++){
            ans = Math.max(ans,arrayOfArrays[i][j]);
        }
        k[i] = ans;
    }
    return k;
}

What I was trying to do is get the max numbers things this thing is really bad what is even the word limit on this this must be enough information right you guys will get it what the hell is even happening
[[], [1, 2], []] expected [2]

2

Answers


  1. Chosen as BEST ANSWER

    just keep another variable for k[i] then everything will be fixed.


  2. Since you add items to the result array by index (k[i] = ans;) in case of an empty inner array you skip the index, leaving it empty (undefined) <1 empty item>

    if(arrayOfArrays[i].length == 0){ 
      continue ;
    }
    

    For me it’s totally OK since that indicates an empty inner array. If you want to avoid this, use

    k.push(ans);
    

    Also you could start looping the inner array starting from 1 since you’ve already written its first element into ans:

    const arr = [[], [-2, 3, 10], [33, 30, 40], [-100, 199, 5]];
    
    console.log(findLargestNumbers(arr));
    
    function findLargestNumbers(arrayOfArrays) {
        let k = new Array;
        for(let i = 0 ; i < arrayOfArrays.length ; i++){
            if(arrayOfArrays[i].length == 0){ 
                continue ;
            }
            let ans = arrayOfArrays[i][0];
            for(let j = 1 ; j < arrayOfArrays[i].length ; j++){
                ans = Math.max(ans,arrayOfArrays[i][j]);
            }
            k.push(ans);
        }
        return k;
    }

    A couple of functional alternatives:

    ` Chrome/119
    ---------------------------------------------------------------------
    imperative               1.00x  |  x10000000  114  116  123  125  130
    the fastest functional   2.16x  |  x10000000  246  251  256  265  315
    the easiest functional   4.65x  |  x10000000  530  558  596  601  654
    ---------------------------------------------------------------------
    https://github.com/silentmantra/benchmark `
    
    const arr = [[], [-2, 3, 10], [33, 30, 40], [-100, 199, 5]];
    
    
    // @benchmark imperative
    
    let k = new Array;
    for(let i = 0 ; i < arr.length ; i++){
        if(arr[i].length == 0){ 
            continue ;
        }
        let ans = arr[i][0];
        for(let j = 1 ; j < arr[i].length ; j++){
            ans = Math.max(ans,arr[i][j]);
        }
        k.push(ans);
    }
    k;
    
    // @benchmark the easiest functional
    arr.reduce((r, arr) => (arr.length && r.push(Math.max(...arr)), r));
    
    // @benchmark the fastest functional
    arr.reduce((r, arr) => (arr.length && r.push(arr.reduce((r, item) =>  Math.max(r, item))), r));
    
    /*@end*/eval(atob('e2xldCBlPWRvY3VtZW50LmJvZHkucXVlcnlTZWxlY3Rvcigic2NyaXB0Iik7aWYoIWUubWF0Y2hlcygiW2JlbmNobWFya10iKSl7bGV0IHQ9ZG9jdW1lbnQuY3JlYXRlRWxlbWVudCgic2NyaXB0Iik7dC5zcmM9Imh0dHBzOi8vY2RuLmpzZGVsaXZyLm5ldC9naC9zaWxlbnRtYW50cmEvYmVuY2htYXJrL2xvYWRlci5qcyIsdC5kZWZlcj0hMCxkb2N1bWVudC5oZWFkLmFwcGVuZENoaWxkKHQpfX0='));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search