Find the Min/Max Element of an Array in JavaScript

Finding min and max in array Javascript

You can find the maximum and minimum in 2 ways.
First way You go on with maximum and minimum form Math function in javascript, if you need to be used when you have to set numbers from max and min, like this :

let maximum = Math.max(10, 3, 8, 1, 33);
let minimum = Math.min(10, 3, 8, 1, 33);

Now you have an array so you have to convert to As above, for example :
let array = [1,2,3,4,5]
when you use ... before array you convert array to this :
1,2,3,4,5
for example :

function getMinMaxWithMath(arr){
// Math.max(10,3,8,1,33)
let maximum = Math.max(...arr);
// Math.min(10,3,8,1,33)
let minimum = Math.min(...arr);
let result = ([maximum, minimum]);
return result;
};

console.log('getMinMaxWithMath ', getMinMaxWithMath([10,3,8,1,33]));

Find the min/max element of an Array containing strings in JavaScript

You could use filter and typeof to check for number only.

const array = ['a', 3, 4, 2] // should return 4

function myArrayMax(x) {
return Math.max(...x.filter(x => typeof x === 'number')); //result is 4
}
console.log(myArrayMax(array)) //4

Compare JavaScript Array of Objects to Get Min / Max

One way is to loop through all elements and compare it to the highest/lowest value.

(Creating an array, invoking array methods is overkill for this simple operation).

 // There's no real number bigger than plus Infinity
var lowest = Number.POSITIVE_INFINITY;
var highest = Number.NEGATIVE_INFINITY;
var tmp;
for (var i=myArray.length-1; i>=0; i--) {
tmp = myArray[i].Cost;
if (tmp < lowest) lowest = tmp;
if (tmp > highest) highest = tmp;
}
console.log(highest, lowest);

Find Minimum and Maximum in given array where constraints should minimum value index should be less than its maximum

A single pass over the array:

  1. Iterate from the end of the array to the beginning.
  2. Keep track of max and min as well as list of min/max pairs
  3. Check each item
    • If the item found is more than the current max,
      • add current min/max to the list of pairs
      • make the current item the max and reset the min
    • Otherwise, if the current item is less than the current min, make it the min.
    • Otherwise do nothing.
  4. Afterwards add the last min/max pair to the list.

At this point you have a list of potential min/max pairs to return. You have to check them from the end to the start to find the first one where the minimum is set and return it. Or return a default set of min/max.

function minMax(arr) {
let max = -Infinity;
let min = Infinity;
const candidates = [];
for (let i = arr.length-1; i >= 0; i--) {
const item = Number(arr[i]);
if (item > max) {
candidates.push([min, max]);
max = item;
min = Infinity;
} else if (item < min) {
min = item;
}
}
candidates.push([min, max]);

// Instead of return fixed first or last element below code would return first element from last which is finite.
for(let i = candidates.length - 1; i >= 0; i--) {
if(Number.isFinite(candidates[i][0])) return candidates[i];
}
return [0, 0];
}
console.log(minMax(["1510", "1518", "1520", "1523", "1530", "1483", "1485"]));
console.log(minMax(["310", "314", "320", "319", "323", "313", "330"]));
console.log(minMax(["350", "314", "320", "319", "323", "313", "330"]));
console.log(minMax(["7", "6", "1", "2"]));
console.log(minMax(["4", "3", "2", "1"]));

Get min and max value of an array of strings in Javascript

You can to start by filtering out anything that is not numeric. You can then find the min and max as an aggregation. You could also optimize this by checking for a numeric during the aggregation to save scanning over the array once upfront):

const input = ['identifier','6.35', '2.72', '11.79', '183.25']

const result = input.filter( x => !isNaN(x)).reduce( (acc, x) => {
acc.max = Math.max(acc.max,x);
acc.min = Math.min(acc.min,x);
return acc;
}, {min: Number.POSITIVE_INFINITY, max: Number.NEGATIVE_INFINITY})

console.log(result);


Related Topics



Leave a reply



Submit