Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Function that returns the minimum and maximum value of an array in JavaScript
Finding the minimum and maximum values in an array is a fundamental task in JavaScript programming. Whether you're analyzing data, implementing algorithms, or building interactive applications, having efficient methods to determine array extremes is essential.
Problem Statement
Create a JavaScript function that takes an array of numbers as input and returns both the minimum and maximum values. The function should handle arrays of any length efficiently.
Sample Input:
const inputArray = [5, 2, 9, 1, 7, 4];
Sample Output:
const minValue = 1; const maxValue = 9;
Using Math.min() and Math.max() with Spread Operator
The simplest approach uses JavaScript's built-in Math.min() and Math.max() functions with the spread operator:
function findMinMax(array) {
const min = Math.min(...array);
const max = Math.max(...array);
return { min, max };
}
const array = [21, 88, 60, 91, 450, 29, 33];
const { min, max } = findMinMax(array);
console.log(`Minimum Element: ${min}`);
console.log(`Maximum Element: ${max}`);
Minimum Element: 21 Maximum Element: 450
Linear Scan Approach
This method iterates through the array once, comparing each element with current min/max values:
function findMinMax(array) {
if (array.length === 0) return { min: undefined, max: undefined };
let min = array[0];
let max = array[0];
for (let i = 1; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
} else if (array[i] > max) {
max = array[i];
}
}
return { min, max };
}
const array = [21, 88, 60, 91, 450, 29, 33];
const { min, max } = findMinMax(array);
console.log(`Minimum Element: ${min}`);
console.log(`Maximum Element: ${max}`);
Minimum Element: 21 Maximum Element: 450
Using Array.reduce()
The reduce() method provides a functional programming approach:
function findMinMax(array) {
return array.reduce(
(acc, current) => ({
min: Math.min(acc.min, current),
max: Math.max(acc.max, current),
}),
{ min: array[0], max: array[0] }
);
}
const array = [21, 88, 60, 91, 450, 29, 33];
const { min, max } = findMinMax(array);
console.log(`Minimum Element: ${min}`);
console.log(`Maximum Element: ${max}`);
Minimum Element: 21 Maximum Element: 450
Sorting Approach
Sort the array and take the first and last elements:
function findMinMax(array) {
const sortedArray = [...array].sort((a, b) => a - b);
return {
min: sortedArray[0],
max: sortedArray[sortedArray.length - 1]
};
}
const array = [21, 88, 60, 91, 450, 29, 33];
const { min, max } = findMinMax(array);
console.log(`Minimum Element: ${min}`);
console.log(`Maximum Element: ${max}`);
Minimum Element: 21 Maximum Element: 450
Divide and Conquer Approach
This recursive method divides the array into smaller parts:
function findMinMax(array) {
if (array.length === 1) {
return { min: array[0], max: array[0] };
}
const mid = Math.floor(array.length / 2);
const left = findMinMax(array.slice(0, mid));
const right = findMinMax(array.slice(mid));
return {
min: Math.min(left.min, right.min),
max: Math.max(left.max, right.max),
};
}
const array = [21, 88, 60, 91, 450, 29, 33];
const { min, max } = findMinMax(array);
console.log(`Minimum Element: ${min}`);
console.log(`Maximum Element: ${max}`);
Minimum Element: 21 Maximum Element: 450
Performance Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Math.min/max with spread | O(n) | O(n) | Small arrays, simplicity |
| Linear scan | O(n) | O(1) | Large arrays, memory efficiency |
| Array.reduce() | O(n) | O(1) | Functional programming style |
| Sorting | O(n log n) | O(n) | When sorted array is needed |
| Divide and conquer | O(n) | O(log n) | Educational purposes |
Conclusion
The linear scan approach offers the best balance of performance and memory efficiency for finding min/max values. For simple cases, Math.min() and Math.max() with the spread operator provide the cleanest code.
