GT.
GT.

Reputation: 792

Get maximum values of a multidimensional array

I'm trying to find the maximum values of a multidimensional array.

Input:

[[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]

Result:

[5, 27, 39, 1001]

What I've tried:

var largestArr = [];

function largestOfFour(arr) {
  for (var i = 0; i < arr.length; i++) {
    var largest = 0;
    var currentArr = arr[i];
    var stop = currentArr.length;
    for (var j = 0; j < stop; j++) {
      if (currentArr[0] > largest) {
        largest = currentArr[0];
        largestArr.push(largest);
        largestArr.shift();
      }
    }

    return largestArr;
  }
}

largestOfFour([
  [4, 5, 1, 3],
  [13, 27, 18, 26],
  [32, 35, 37, 39],
  [1000, 1001, 857, 1]
]);

Can anyone help me understand why this isn't working?

Upvotes: 0

Views: 47

Answers (3)

thedarklord47
thedarklord47

Reputation: 3302

Christos' answer is correct and should the selected as answer.

Additionally though, you should not pollute the global namespace by declaring largestArray outside of the function, considering that you are returning said array anyway.

function largestOfFour(arr) {
    var largestArr = [];       // put this line here instead of making it global
    for (var i=0; i<arr.length; i++) {
        var largest = 0;
        var currentArr = arr[i];
        var stop = currentArr.length;
        for (var j=0; j<stop; j++){
            if (currentArr[j]>largest){
                largest = currentArr[0]; 
            }
        }
        largestArr.push(largest);
    }
    return largestArr;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

Upvotes: 0

Christos
Christos

Reputation: 53958

The problem is here

if (currentArr[0]>largest){
   largest = currentArr[0];
   //
}

This should be done like below:

if (currentArr[j]>largest){
    largest = currentArr[j];
    // 
}

Why this is wrong?

This is wrong because you always compare the first item of each array with the largest and not all the items in each array one after the other.

Furthermore, you have to make a few more corrections.

Specifficaly, there isn't any need to use the shift method. You have only to push the largest value of each array after the end of each processing in the largestArr. Last, but not least you have to move the return statement of the largestArr out of the for loops.

var largestArr = [];

function largestOfFour(arr) {
  for (var i=0; i<arr.length; i++) {
    var largest = 0;
    var currentArr = arr[i];
    var stop = currentArr.length;
    for (var j=0; j<stop; j++){
      if (currentArr[j]>largest){
        largest = currentArr[j];
      }
    }
    largestArr.push(largest); 
  }
  return largestArr;
}

alert(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));

Upvotes: 1

Andy
Andy

Reputation: 63524

Since the answer has been provided here's some code can do the same job in a smaller number of lines that you might find useful in the future using reduce.

var out = arr.reduce(function (p, c) {
  return p.concat(Math.max.apply(null, c));
}, []);

DEMO

Upvotes: 0

Related Questions