10

I have a multidimensional array of x columns and y rows. How can I find the min and the max value of the matrix? Example:

[[1,  37.8, 80.8, 41.8],
[2,  30.9, 69.5, 32.4],
[3,  25.4,   57, 25.7],
[4,  11.7, 18.8, 10.5],
[5,  11.9, 17.6, 10.4],
[6,   8.8, 13.6,  7.7],
[7,   7.6, 12.3,  9.6],
[8,  12.3, 29.2, 10.6],
[9,  16.9, 42.9, 14.8],
[10, 12.8, 30.9, 11.6],
[11,  5.3,  7.9,  4.7],
[12,  6.6,  8.4,  5.2],
[13,  4.8,  6.3,  3.6],
[14,  4.2,  6.2,  3.4]]
Pani
  • 1,317
  • 1
  • 14
  • 20
provola
  • 342
  • 1
  • 3
  • 15
  • show that *multidimensional array* at start – RomanPerekhrest Sep 06 '16 at 07:04
  • put whole code , that you tried, if you have not tried, no one is there to do work for you. – yash Sep 06 '16 at 07:08
  • Possible duplicate of [Get largest value in multi-dimensional array javascript or coffeescript](http://stackoverflow.com/questions/11149843/get-largest-value-in-multi-dimensional-array-javascript-or-coffeescript) – Shailendra Sharma Sep 06 '16 at 07:11
  • Please share the exact example of multidimensional array. This is not a proper multidimensional array – brk Sep 06 '16 at 07:15

10 Answers10

12

let

var arr = [[2,3], [4,5]]; // a multidimensional array

then get an array with each row's maximum with

var maxRow = arr.map(function(row){ return Math.max.apply(Math, row); });

and the overal maximum with

var max = Math.max.apply(null, maxRow);
Pani
  • 1,317
  • 1
  • 14
  • 20
8

Regardless of the dimension of the array, i believe this is the way to get the max of all primitives involved.

function getMax(a){
  return Math.max(...a.map(e => Array.isArray(e) ? getMax(e) : e));
}

var arr = [[1,  37.8, 80.8, 41.8],
           [2,  30.9, 69.5, 32.4],
           [3,  25.4,   57, 25.7],
           [4,  11.7, 18.8, 10.5],
           [5,  11.9, 17.6, 10.4],
           [6,   8.8, 13.6,  7.7],
           [7,   7.6, 12.3,  9.6],
           [8,  12.3, 29.2, 10.6],
           [9,  16.9, 42.9, 14.8],
           [10, 12.8, 30.9, 11.6],
           [11,  5.3,  7.9,  4.7],
           [12,  6.6,  8.4,  5.2],
           [13,  4.8,  6.3,  3.6],
           [14,  4.2,  6.2,  3.4]];
 console.log(getMax(arr));

It should work on arrays with indefinite dimension.

function getMax(a){
  return Math.max(...a.map(e => Array.isArray(e) ? getMax(e) : e));
}

var arr = [[1,  37.8, 80.8, 41.8],
           [2,  30.9, 69.5, 32.4],
           [3,  25.4,   57, 25.7],
           [4,  11.7, 18.8, 10.5],
           [5,  11.9, 17.6, 10.4],
           [6,   8.8, 13.6,  7.7],
           [7,   7.6, 12.3,  9.6],
           [8,  12.3, 29.2, 10.6],
           [9,  16.9, 42.9, 14.8],
           [10, 12.8, 30.9, 11.6],
           [11,  5.3,  [6.1,[56.7,[98.55]]],  4.7],
           [12,  6.6,  8.4,  5.2],
           [13,  4.8,  6.3,  3.6],
           [14,  4.2,  6.2,  3.4]];
 console.log(getMax(arr));
Redu
  • 25,060
  • 6
  • 56
  • 76
6

Simplest Solution

Flatten and use the usual Math.max with spread operator.

Math.max(...arr.flat())

const arr = [[1,  37.8, 80.8, 41.8],
           [2,  30.9, 69.5, 32.4],
           [3,  25.4,   57, 25.7],
           [4,  11.7, 18.8, 10.5],
           [5,  11.9, 17.6, 10.4],
           [6,   8.8, 13.6,  7.7],
           [7,   7.6, 12.3,  9.6],
           [8,  12.3, 29.2, 10.6],
           [9,  16.9, 42.9, 14.8],
           [10, 12.8, 30.9, 11.6],
           [11,  5.3,  7.9,  4.7],
           [12,  6.6,  8.4,  5.2],
           [13,  4.8,  6.3,  3.6],
           [14,  4.2,  6.2,  3.4]];

Math.max(...arr.flat())
Vikram Deshmukh
  • 12,304
  • 4
  • 36
  • 38
5

You can get max value of multi dimension array using following method:

var arr = [[1, 5,6], [4, 7,8], [3, 8,20], [2, 3,1],[12, 4,5]];

console.log(Math.max.apply(Math, arr.map(function (i) {
    return i[0]+i[1]+i[2];
})));

It first use array.map() to convert the multi-dimensional array to a flat one, and after that use Math.max().

Umair Malik
  • 1,403
  • 3
  • 30
  • 59
4

Most answers here use apply or the spread operator ... to call the Math.max function with all the elements of an array as parameters.

For large arrays it is safer to use reduce:

// returns maximum of an array
const getArrayMax = array => array.reduce((a, b) => Math.max(a, b));

// returns maximum of a 2D array
const getArrayMax2d = array2d => getArrayMax(array2d.map(getArrayMax));
Rafi
  • 816
  • 9
  • 21
  • 1
    You can do ```getArrayMax(array2d.map(getArrayMax));``` instead of ```getArrayMax(array2d.map(row => getArrayMax(row)));``` – dll Feb 22 '18 at 15:51
1

based on this answer, you can do it in 1 line (assuming ES6):

const arr = [[12,45,75], [54,45,2],[23,54,75,2]];

const max = Math.max(...[].concat(...arr));

const min = Math.min(...[].concat(...arr));

console.log(max);

console.log(min);
DankMasterDan
  • 1,900
  • 4
  • 23
  • 35
0

The solution using Array.prototype.push, Math.min and Math.max methods:

// arr is your initial array
var flattened = [], minValue, maxValue;
arr.forEach(function (v) {
    Array.prototype.push.apply(flattened, v);
});

minValue = Math.min.apply(null, flattened);
maxValue = Math.max.apply(null, flattened);

console.log('min: ' + minValue, 'max: ' + maxValue);  // min: 1 max: 80.8

DEMO link

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

You can also achieve this with a reduction (it's slower but if the array is not huge, it doesn't matter) resulting in a object containing both the min and the max like so:

matrix.reduce(function (res, item) {
    item.forEach(function (val) {
        if (!res.hasOwnProperty('max') || val > res.max) res.max = val;
        if (!res.hasOwnProperty('min') || val < res.min) res.min = val;
    });
    return res;
}, {});
Olli K
  • 1,720
  • 1
  • 16
  • 17
0

If i could i would comment this on DankMasterDan's answer

let a = [[12,45,75], [54,45,2],[23,54,75,2]];
let max = Math.max(...a.map(r=>Math.max(...r)));

this may avoid the stack size exceeded error. David mentioned since its is batching each row with the map first.

Exo Flame
  • 97
  • 4
0

I found this way to your solution very simple way

function largestOfFour(arr) {
  let maxValue;
  let arrMax = []
for(let i = 0 ; i < arr.length ; i++){
maxValue = Math.max.apply(null ,arr[i])
arrMax.push(maxValue)
}
  return arrMax;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
 return --> [ 5, 27, 39, 1001 ]

Tyron
  • 1,141
  • 1
  • 4
  • 10