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
Finding the largest and smallest number in an unsorted array of integers in JavaScript
In JavaScript, finding the largest and smallest numbers in an unsorted array is a common programming task. We can efficiently solve this using a linear scan approach without sorting the entire array.
Understanding the Problem
Given an unsorted array of integers, we need to find both the minimum and maximum values. For example, in the array [11, 21, 14, 32, 20, 12], the smallest number is 11 and the largest is 32. The challenge is to find these values efficiently without sorting the array first.
Method 1: Linear Scan Approach
The most straightforward approach uses a single pass through the array, comparing each element with our current minimum and maximum values.
Algorithm
Step 1: Initialize variables to track the smallest and largest values using the first array element.
Step 2: Iterate through the remaining array elements starting from index 1.
Step 3: Compare each element with current smallest and largest values, updating them when necessary.
Step 4: Return both values after the complete iteration.
function findMinMax(arr) {
if (arr.length === 0) {
return { min: null, max: null };
}
let min = arr[0];
let max = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
}
return { min, max };
}
const numbers = [4, 2, 9, 1, 7, 5, 12, 3];
const result = findMinMax(numbers);
console.log("Smallest:", result.min);
console.log("Largest:", result.max);
Smallest: 1 Largest: 12
Method 2: Using Math.min() and Math.max()
JavaScript provides built-in Math.min() and Math.max() functions that can be used with the spread operator for a more concise solution.
function findMinMaxBuiltIn(arr) {
if (arr.length === 0) {
return { min: null, max: null };
}
const min = Math.min(...arr);
const max = Math.max(...arr);
return { min, max };
}
const numbers = [15, 3, 8, 1, 22, 7];
const result = findMinMaxBuiltIn(numbers);
console.log("Using Math functions:");
console.log("Smallest:", result.min);
console.log("Largest:", result.max);
Using Math functions: Smallest: 1 Largest: 22
Method 3: Using Array.reduce()
The reduce() method provides a functional programming approach to find both values in a single pass.
function findMinMaxReduce(arr) {
if (arr.length === 0) {
return { min: null, max: null };
}
return arr.reduce(
(acc, current) => ({
min: Math.min(acc.min, current),
max: Math.max(acc.max, current)
}),
{ min: arr[0], max: arr[0] }
);
}
const numbers = [25, 8, 16, 3, 19, 12];
const result = findMinMaxReduce(numbers);
console.log("Using reduce:");
console.log("Smallest:", result.min);
console.log("Largest:", result.max);
Using reduce: Smallest: 3 Largest: 25
Comparison
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| Linear Scan | O(n) | O(1) | High |
| Math.min/max | O(n) | O(n)* | Very High |
| Array.reduce | O(n) | O(1) | Medium |
*Math.min/max with spread operator creates a temporary copy of the array
Key Points
- All methods have O(n) time complexity as each element must be examined at least once
- Linear scan is most memory-efficient with O(1) space complexity
- Built-in Math functions offer the cleanest syntax but use more memory
- Always handle empty array cases to avoid errors
Conclusion
The linear scan approach provides the best balance of efficiency and readability for finding minimum and maximum values. For simple cases, Math.min/max with spread operator offers cleaner syntax, while reduce() provides a functional programming alternative.
