0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Find Largest Element in Array

You can find the largest element in an array using Math.max(...array) or by looping through the array and comparing elements with a variable holding the current largest value.
📋

Examples

Input[3, 5, 1, 9, 2]
Output9
Input[-10, -5, -3, -1]
Output-1
Input[7]
Output7
🧠

How to Think About It

To find the largest element, start by assuming the first element is the largest. Then check each element one by one. If you find an element bigger than the current largest, update your largest value. At the end, the largest value you have is the biggest element in the array.
📐

Algorithm

1
Get the input array.
2
Set the first element as the largest value.
3
Go through each element in the array starting from the second.
4
If the current element is greater than the largest value, update the largest value.
5
After checking all elements, return the largest value.
💻

Code

javascript
const arr = [3, 5, 1, 9, 2];
let largest = arr[0];
for (let i = 1; i < arr.length; i++) {
  if (arr[i] > largest) {
    largest = arr[i];
  }
}
console.log(largest);
Output
9
🔍

Dry Run

Let's trace the array [3, 5, 1, 9, 2] through the code to find the largest element.

1

Initialize largest

largest = 3 (first element)

2

Compare with second element

5 > 3, so largest = 5

3

Compare with third element

1 > 5? No, largest stays 5

4

Compare with fourth element

9 > 5, so largest = 9

5

Compare with fifth element

2 > 9? No, largest stays 9

6

Return largest

largest = 9

IterationCurrent ElementLargest Value
133
255
315
499
529
💡

Why This Works

Step 1: Start with first element

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

Step 2: Compare each element

We check each element using if (arr[i] > largest) to find if it is bigger than the current largest.

Step 3: Update largest value

When a bigger element is found, we update the largest variable to hold this new value.

Step 4: Return the largest

After checking all elements, the largest variable holds the biggest number in the array.

🔄

Alternative Approaches

Using Math.max with spread operator
javascript
const arr = [3, 5, 1, 9, 2];
const largest = Math.max(...arr);
console.log(largest);
This is shorter and uses built-in functions but may be less clear for beginners.
Using Array.reduce method
javascript
const arr = [3, 5, 1, 9, 2];
const largest = arr.reduce((max, current) => current > max ? current : max, arr[0]);
console.log(largest);
This uses functional programming style and is concise but may be harder to understand at first.

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 and Math.max approaches both run in O(n) time, but Math.max may be slower for very large arrays due to spreading.

ApproachTimeSpaceBest For
Loop with variableO(n)O(1)Clear logic and large arrays
Math.max with spreadO(n)O(n)Short code and small to medium arrays
Array.reduceO(n)O(1)Functional style and chaining
💡
Use Math.max(...array) for a quick and simple way to find the largest element.
⚠️
Beginners often forget to start the largest value with the first element, causing errors when the array has all negative numbers.