Lesson 35-Finding the Maximum and Minimum Values in a JavaScript Array

Using Math.max and Math.min with Spread Operator

const array = [1, 0.5, 3, 4.5, -2, 6, -7];

const max = Math.max(...array);
const min = Math.min(...array);

console.log(max); // Output: 6
console.log(min); // Output: -7

This method uses the spread operator (...) to pass array elements as individual arguments to Math.max and Math.min, which return the largest and smallest values, respectively. It works well when all array elements are numbers. Non-numeric elements are converted to NaN, which may lead to unexpected results.

Using reduce Method

const array = [1, 0.5, 3, 4.5, -2, 6, -7];

const max = array.reduce((acc, curr) => Math.max(acc, curr), Number.NEGATIVE_INFINITY);
const min = array.reduce((acc, curr) => Math.min(acc, curr), Number.POSITIVE_INFINITY);

console.log(max); // Output: 6
console.log(min); // Output: -7

The reduce method applies an accumulator function with an initial value. Here, the accumulator uses Math.max or Math.min to compare the current element with the accumulated value, returning the larger or smaller value. Initializing with Number.NEGATIVE_INFINITY and Number.POSITIVE_INFINITY ensures reasonable results even for empty arrays.

Using sort Method

const array = [1, 0.5, 3, 4.5, -2, 6, -7];

const sortedArray = array.slice().sort((a, b) => a - b);
const max = sortedArray[sortedArray.length - 1];
const min = sortedArray[0];

console.log(max); // Output: 6
console.log(min); // Output: -7

This approach creates a copy of the array using slice (to avoid modifying the original), then sorts it with a comparison function (a, b) => a - b for numerical ordering. The last element of the sorted array is the maximum, and the first is the minimum.

Custom Loop Iteration

const array = [1, 0.5, 3, 4.5, -2, 6, -7];

let max = Number.NEGATIVE_INFINITY;
let min = Number.POSITIVE_INFINITY;

for (const num of array) {
  if (num > max) max = num;
  if (num < min) min = num;
}

console.log(max); // Output: 6
console.log(min); // Output: -7

This method iterates through the array, comparing each element with the current maximum and minimum, updating them as needed. Initializing max to Number.NEGATIVE_INFINITY and min to Number.POSITIVE_INFINITY ensures any array element can override the initial values.

Extending Array.prototype with Custom Methods

Array.prototype.max = function() {
  return Math.max(...this);
};

Array.prototype.min = function() {
  return Math.min(...this);
};

const array = [1, 0.5, 3, 4.5, -2, 6, -7];

const max = array.max();
const min = array.min();

console.log(max); // Output: 6
console.log(min); // Output: -7

This approach adds max and min methods to Array.prototype, allowing all array instances to call them directly. However, modifying built-in prototypes can cause conflicts with other libraries or future JavaScript versions, so it’s generally discouraged unless necessary.

Using Array.from with Math.max/Math.min

const array = [1, 0.5, 3, 4.5, -2, 6, -7];

const max = Math.max(...Array.from(array, Number));
const min = Math.min(...Array.from(array, Number));

console.log(max); // Output: 6
console.log(min); // Output: -7

Array.from converts array-like or iterable objects into arrays. Here, it uses the Number mapping function to ensure all elements are numbers, then applies the spread operator with Math.max/Math.min to find the maximum and minimum.

Using Array.prototype.map with Math.max/Math.min

const array = [1, 0.5, 3, 4.5, -2, 6, -7];

const max = Math.max(...array.map(Number));
const min = Math.min(...array.map(Number));

console.log(max); // Output: 6
console.log(min); // Output: -7

Array.prototype.map creates a new array by applying the Number function to each element, ensuring numerical values. The spread operator then passes these to Math.max/Math.min to compute the maximum and minimum.

Using Array.prototype.filter with Math.max/Math.min (for Arrays with Mixed Types)

const array = [1, 'a', 0.5, 3, 'b', 4.5, -2, 6, 'c', -7];

const numbersOnly = array.filter(Number.isFinite);
const max = Math.max(...numbersOnly);
const min = Math.min(...numbersOnly);

console.log(max); // Output: 6
console.log(min); // Output: -7

Array.prototype.filter creates a new array with elements passing the Number.isFinite test, ensuring only numbers remain. The spread operator then applies Math.max/Math.min to find the maximum and minimum.

Using for…of Loop with break Statement (for Early Termination)

const array = [1, 0.5, 3, 4.5, -2, 6, -7];

let max = Number.NEGATIVE_INFINITY;
let min = Number.POSITIVE_INFINITY;

for (const num of array) {
  if (num > max) {
    max = num;
    break; // Stop after finding max
  }
  if (num < min) {
    min = num;
    break; // Stop after finding min
  }
}

console.log(max); // Output: 6
console.log(min); // Output: -7

This method stops iterating after finding the first maximum or minimum, suitable for sorted data or when only one value is needed. However, it only finds one maximum or minimum if multiple exist.

Share your love