JavaScript Program to Find Second Largest Number in Array
You can find the second largest number in an array in JavaScript by first removing duplicates with
new Set(), then sorting the array with sort((a,b) => b - a), and finally accessing the second element with [1].Examples
Input[3, 5, 1, 2, 4]
Output4
Input[10, 10, 9, 8]
Output9
Input[5]
Outputundefined
How to Think About It
To find the second largest number, first think about removing any repeated numbers so they don't confuse the order. Then, arrange the numbers from biggest to smallest. The second number in this sorted list is the second largest. If the list is too short, there might not be a second largest number.
Algorithm
1
Remove duplicate numbers from the array.2
Sort the array in descending order (largest to smallest).3
Check if the array has at least two elements.4
Return the second element as the second largest number.5
If not enough elements, return undefined or a message.Code
javascript
const findSecondLargest = arr => { const uniqueArr = [...new Set(arr)]; uniqueArr.sort((a, b) => b - a); return uniqueArr.length > 1 ? uniqueArr[1] : undefined; }; console.log(findSecondLargest([3, 5, 1, 2, 4])); console.log(findSecondLargest([10, 10, 9, 8])); console.log(findSecondLargest([5]));
Output
4
9
undefined
Dry Run
Let's trace the array [3, 5, 1, 2, 4] through the code
1
Remove duplicates
Input array: [3, 5, 1, 2, 4] → Unique array: [3, 5, 1, 2, 4]
2
Sort descending
Sorted array: [5, 4, 3, 2, 1]
3
Return second largest
Second element: 4
| Step | Unique Array | Sorted Array | Second Largest |
|---|---|---|---|
| Initial | [3,5,1,2,4] | N/A | N/A |
| After removing duplicates | [3,5,1,2,4] | N/A | N/A |
| After sorting | [3,5,1,2,4] | [5,4,3,2,1] | N/A |
| Result | [3,5,1,2,4] | [5,4,3,2,1] | 4 |
Why This Works
Step 1: Remove duplicates
Using new Set() removes repeated numbers so sorting and indexing work correctly.
Step 2: Sort array descending
Sorting with (a, b) => b - a puts the largest numbers first.
Step 3: Access second element
The second element in the sorted array is the second largest number.
Alternative Approaches
Iterative approach without sorting
javascript
function findSecondLargest(arr) { let first = -Infinity, second = -Infinity; for (const num of arr) { if (num > first) { second = first; first = num; } else if (num > second && num < first) { second = num; } } return second === -Infinity ? undefined : second; } console.log(findSecondLargest([3,5,1,2,4]));
This method finds the second largest in one pass without sorting, which is faster for large arrays but more complex to understand.
Using built-in Math functions
javascript
const findSecondLargest = arr => { const uniqueArr = [...new Set(arr)]; const max = Math.max(...uniqueArr); const filtered = uniqueArr.filter(n => n !== max); return filtered.length ? Math.max(...filtered) : undefined; }; console.log(findSecondLargest([3,5,1,2,4]));
This uses Math.max twice and filtering, which is clear but less efficient than sorting once.
Complexity: O(n log n) time, O(n) space
Time Complexity
Sorting the array takes O(n log n) time, which dominates the runtime. Removing duplicates and accessing elements are O(n).
Space Complexity
Creating a new array for unique elements uses O(n) extra space.
Which Approach is Fastest?
The iterative approach without sorting runs in O(n) time and uses O(1) space, making it fastest for large arrays but slightly harder to read.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Sort and pick second | O(n log n) | O(n) | Simplicity and small to medium arrays |
| Iterative one-pass | O(n) | O(1) | Large arrays and performance |
| Math.max with filter | O(n) | O(n) | Readability with built-in functions |
Always remove duplicates first to avoid errors when finding the second largest number.
Beginners often forget to remove duplicates, causing the largest number to appear twice and giving wrong results.