I have js function to find min and max values in 2d array which works ok on small arrays but when I pass it big array it give me range error
:
Maximum call stack size exceeded.
I am using the latest version of Chrome.
function MaxMin2dray(arr, idx){
return {
min: Math.min.apply(null, arr.map(function (e) { return e[idx]})),
max: Math.max.apply(null, arr.map(function (e) { return e[idx]}))
}
}
function minMax2DArray(arr, idx) {
var max = -Number.MAX_VALUE,
min = Number.MAX_VALUE;
arr.forEach(function(e) {
if (max < e[idx]) {
max = e[idx];
}
if (min > e[idx]) {
min = e[idx];
}
});
return {max: max, min: min};
}
👆 Edit: Removed use of MIN_VALUE
(see post below).
const arrayMinMax = (arr) =>
arr.reduce(([min, max], val) => [Math.min(min, val), Math.max(max, val)], [
Number.POSITIVE_INFINITY,
Number.NEGATIVE_INFINITY,
]);
The Math.min
and Math.max
most likely crash, or return NaN
for big arrays (~10⁷)
(see @DavidWaters & @EugeneGluhotorenko comments).
Instead, you can use old javascript loops like so:
(2nd func is much faster)
function getMax(arr) {
return arr.reduce((max, v) => max >= v ? max : v, -Infinity);
}
Or
function getMax(arr) {
let len = arr.length;
let max = -Infinity;
while (len--) {
max = arr[len] > max ? arr[len] : max;
}
return max;
}
There is a issue in bjornl's answer. According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_VALUE
The MIN_VALUE property is the number closest to 0, not the most negative number, that JavaScript can represent.
The updated code:
function minMax2DArray(arr, idx) {
var max = -Number.MAX_VALUE,
min = Number.MAX_VALUE;
arr.forEach(function(e) {
if (max < e[idx]) {
max = e[idx];
}
if (min > e[idx]) {
min = e[idx];
}
});
return {max: max, min: min};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With