0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Find Smallest Element in Array

You can find the smallest element in an array using Math.min(...array) or by looping through the array and comparing elements to track the smallest value.
📋

Examples

Input[3, 1, 4, 1, 5]
Output1
Input[10, 20, 30, 5, 15]
Output5
Input[-2, -5, 0, 3]
Output-5
🧠

How to Think About It

To find the smallest element, look at each number in the array one by one and remember the smallest number you have seen so far. At the end, the remembered number is the smallest.
📐

Algorithm

1
Get the input array.
2
Set the first element as the smallest number.
3
Go through each element in the array.
4
If the current element is smaller than the remembered smallest, update the smallest.
5
After checking all elements, return the smallest number.
💻

Code

javascript
const array = [3, 1, 4, 1, 5];
let smallest = array[0];
for (let i = 1; i < array.length; i++) {
  if (array[i] < smallest) {
    smallest = array[i];
  }
}
console.log(smallest);
Output
1
🔍

Dry Run

Let's trace the array [3, 1, 4, 1, 5] through the code to find the smallest element.

1

Initialize smallest

smallest = 3 (first element)

2

Compare with second element

1 < 3, so smallest = 1

3

Compare with third element

4 < 1? No, smallest stays 1

4

Compare with fourth element

1 < 1? No, smallest stays 1

5

Compare with fifth element

5 < 1? No, smallest stays 1

6

Return smallest

smallest = 1

IterationCurrent ElementSmallest So Far
133
211
341
411
551
💡

Why This Works

Step 1: Start with first element

We assume the first element is the smallest to have a starting point for comparison.

Step 2: Compare each element

We check each element to see if it is smaller than the current smallest value.

Step 3: Update smallest value

If a smaller element is found, we update the smallest value to that element.

Step 4: Return the smallest

After checking all elements, the smallest value holds the smallest element in the array.

🔄

Alternative Approaches

Using Math.min with spread operator
javascript
const array = [3, 1, 4, 1, 5];
const smallest = Math.min(...array);
console.log(smallest);
This is a concise and readable method but may fail with very large arrays due to argument limits.
Using Array.reduce method
javascript
const array = [3, 1, 4, 1, 5];
const smallest = array.reduce((min, current) => current < min ? current : min, array[0]);
console.log(smallest);
This method is functional and elegant, good for those familiar with reduce.

Complexity: O(n) time, O(1) space

Time Complexity

The program checks each element once, so the time grows linearly with the array size, making it O(n).

Space Complexity

Only a few variables are used regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

The loop method and reduce have similar performance; Math.min is concise but may be slower or limited on very large arrays.

ApproachTimeSpaceBest For
Loop with ifO(n)O(1)Large arrays, simple logic
Math.min with spreadO(n)O(n)Small to medium arrays, concise code
Array.reduceO(n)O(1)Functional style, readability
💡
Use Math.min(...array) for quick and simple smallest element retrieval in small to medium arrays.
⚠️
Forgetting to initialize the smallest value before comparing leads to incorrect results or errors.