Find minimum and maximum elements of an array in JavaScript
This post will discuss how to find the minimum and the maximum element of an array in JavaScript.
1. Using Math.min() and Math.max() Methods
One of the simplest methods to find the minimum and maximum elements of an array in JavaScript is to use the Math.min() and Math.max() methods. The Math.min() method returns the smallest of zero or more numbers, while the Math.max() method returns the largest of zero or more numbers. Since these methods do not accept an array as an argument directly, we would need to use the spread operator (…), which expands an iterable into individual arguments. For example:
|
1 2 3 4 5 |
let numbers = [6, 3, 5, 2, 9]; // Find the minimum and maximum of the array using the spread operator console.log(Math.min(...numbers)); // 2 console.log(Math.max(...numbers)); // 9 |
Alternatively, we can use the apply() method, which calls a function with a given this value and an array of arguments. This can be used for arrays with fewer elements. For example:
|
1 2 3 4 5 |
let numbers = [6, 3, 5, 2, 9]; // Find the minimum and maximum of the array using the apply method console.log(Math.min.apply(null, numbers)); // 2 console.log(Math.max.apply(null, numbers)); // 9 |
This method works with any kind of values in the array that can be compared numerically, such as numbers or strings that can be parsed as numbers. However, it cannot work with values that cannot be compared numerically, such as array of objects.
2. Using a loop
Another way to find the minimum and maximum elements of an array in JavaScript is to use a for loop or a forEach loop. To find the minimum and maximum elements of an array using a for loop or a forEach loop, we can loop through the elements of the array and compare each element with the current minimum and maximum values. We update the minimum and maximum values if a smaller or larger element is found. Here is an example of how to achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
let numbers = [6, 3, 5, 2, 9]; // Declare two variables that will store the minimum and maximum values let min = Infinity, max = -Infinity; // Loop through the elements of the array using a for loop for (let num of numbers) { // Compare the current element with the current minimum value if (num < min) { // Update the minimum value if a smaller element is found min = num; } // Compare the current element with the current maximum value if (num > max) { // Update the maximum value if a larger element is found max = num; } } // Log the minimum and maximum values console.log(min, max); // 2 9 |
This method works with any kind of values in the array that can be compared using the less than (<) or greater than (>) operators, such as numbers or strings. This method can also be used to compare an array of objects, based on a numeric property. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// An array of objects with a value property let objects = [{value: 1}, {value: 2}, {value: 3}, {value: 4}]; let min = objects[0], max = objects[0]; for (let obj of objects) { if (obj["value"] < min["value"]) { min = obj; } if (obj["value"] > max["value"]) { max = obj; } } console.log(min, max); // { value: 1 } { value: 4 } |
This method is customizable and adaptable, as we can use different comparison criteria or operators according to our needs. However, we need to be aware of the performance and memory usage of our code, as looping through large arrays can be slow and costly.
3. Using the Array.prototype.reduce() method
Another way to find the minimum and maximum elements of an array in JavaScript is to use the Array.prototype.reduce() method. This method applies a function to each element of an array and accumulates the result in a single value. The function takes four arguments: an accumulator, a current value, a current index, and an array. It returns a new accumulator value that will be used in the next iteration. To find the minimum and maximum elements of an array using reduce(), we need to follow these steps:
- Define a function that compares two values and returns the smaller or larger one.
- Apply the function to each element of the array using
reduce()and pass an initial value that is either very large or very small. - Get the final accumulator value that will be either the minimum or maximum element of the array.
Here is an example of how to implement these steps in JavaScript using a function that returns the smaller and larger value:
|
1 2 3 4 5 6 7 8 |
let numbers = [6, 3, 5, 2, 9]; // Apply the function to each element of the array using `reduce()` let min = numbers.reduce((x, y) => Math.min(x, y), Infinity); let max = numbers.reduce((x, y) => Math.max(x, y), -Infinity); // Log the minimum and maximum values console.log(min, max); // 2 9 |
This method works with any kind of values in the array that can be compared using a custom function, such as numbers, strings, objects, or arrays. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Define an array of objects with a value property let objects = [{value: 1}, {value: 2}, {value: 3}, {value: 4}]; // Define a function that compares two objects by their value property // and returns the smaller one function minByValue(a, b) { return a.value < b.value ? a : b; } // Define a function that compares two objects by their value property // and returns the larger one function maxByValue(a, b) { return a.value > b.value ? a : b; } // Apply the function to each element of the array using `reduce()` let min = objects.reduce(minByValue, {value: Infinity}); let max = objects.reduce(maxByValue, {value: -Infinity}); // Log the minimum and maximum objects console.log(min, max); // { value: 1 } { value: 4 } |
That's all about finding the minimum and maximum elements of an array in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)