Open In App

Find the min/max element of an Array using JavaScript

Improve
Improve
Like Article
Like
Save
Share
Report

We will be given an array and we need to find out the minimum and maximum element from all of the given elements. There are many methods to find out the min and max elements and those methods’ performance depends on the length of the array.

The minimum and maximum elements in an array can be found using the following methods:

Method 1: Using Math.min() and Math.max() Methods 

The Math object’s min() and max() methods are static methods that return the minimum and maximum elements of a given array. The spread(…) operator could pass these functions into an array. The spread operator allows an iterable to expand in places where multiple arguments are expected. In this case, it automatically expands the array and gives the numbers to the functions. 

Syntax:

minValue = Math.min(...array);
maxValue = Math.max(...array);

Example: We will see the basic implementation of JavaScript Math.min() and Math.max() methods.

Javascript




function findMinMax() {
    let Arr = [50, 60, 20, 10, 40];
 
    let minValue = Math.min(...Arr);
    let maxValue = Math.max(...Arr);
     
    console.log("Minimum element is:" + minValue);
    console.log("Maximum Element is:" + maxValue);
}
 
findMinMax()


Output

Minimum element is:10
Maximum Element is:60

Method 2: Iterating through the Array 

Iterating through the array and keeping track of the minimum and maximum elements. The minimum and maximum element can be kept track by iterating through all the elements in the array and updating the minimum and maximum element up to that point by comparing them to the current minimum and maximum values. The minimum and maximum values are initialized to Infinity and -Infinity. 

Syntax:

minValue = Infinity;
maxValue = -Infinity;
for (item of array) {
// Find minimum value
if (item < minValue)
minValue = item;

// Find maximum value
if (item > maxValue)
maxValue = item;
}

Example: We will be iterating through the elements of the array to find the max and min elements from the array.

Javascript




function findMinMax() {
    let Arr = [50, 60, 20, 10, 40];
    let minValue = Infinity;
    let maxValue = -Infinity;
 
    for (let item of Arr) {
 
        // Find minimum value
        if (item < minValue)
            minValue = item;
 
        // Find maximum value
        if (item > maxValue)
            maxValue = item;
    }
 
    console.log("Minimum element is:" + minValue);
    console.log("Minimum element is:" + maxValue);
}
 
findMinMax();


Output

Minimum element is:10
Minimum element is:60

Method 3: Using JavaScript Array.reduce() method

The Javascript arr.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator. 

Syntax: 

array.reduce( function(total, currentValue, currentIndex, arr), 
initialValue )

Example: we are Using JavaScript Array.reduce() method

Javascript




// Input array
let Arr = [50, 60, 20, 10, 40];
 
// Getting min value
let minValue = Arr.reduce((acc, current) => Math.min(acc, current));
 
// Getting max value
let maxValue = Arr.reduce((acc, current) => Math.max(acc, current));
 
// Display output
 console.log("Minimum element is:" + minValue);
 console.log("Minimum element is:" + maxValue);


Output

Minimum element is:10
Minimum element is:60

Method 4: Using apply method

Using apply( ) method we can invoke a given function with different objects.

Syntax:

object.objectMethod.apply(objectInstance, arrayOfArguments)

Parameters:

  • ObjectInstance: It is an object which we want to use explicitly
  • Arguments: It is arguments that we want to pass to the calling function

Example: We are using apply method.

Javascript




// Sample array
const numbers = [5, 2, 8, 1, 4];
 
// Using apply method to find the minimum element
const minElement = Math.min.apply(null, numbers);
console.log('Minimum Element:', minElement);
 
// Using apply method to find the maximum element
const maxElement = Math.max.apply(null, numbers);
console.log('Maximum Element:', maxElement);


Output

Minimum Element: 1
Maximum Element: 8

Method 5: Using spread operator

The Spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 value is expected. It allows us the privilege to obtain a list of parameters from an array. 

The syntax of the Spread operator is the same as the Rest parameter but it works completely opposite of it. 

Syntax:

let variablename1 = [...value]; 

Example: We are using spread operator.

Javascript




// Sample array
const numbers = [5, 2, 8, 1, 4];
 
// Using spread operator to find the minimum element
const minElement = Math.min(...numbers);
console.log('Minimum Element:', minElement);
 
// Using spread operator to find the maximum element
const maxElement = Math.max(...numbers);
console.log('Maximum Element:', maxElement);


Output

Minimum Element: 1
Maximum Element: 8



Last Updated : 01 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads