09. Find the Second Largest Element
Find the second largest distinct element in an array without sorting.
How It Works & Algorithmic Design
This uses a Dual-Variable Tracking approach in a single pass:
- Variables: Maintain
first(largest) andsecond(second largest), initialized to negative infinity (-Infinity). - Iteration & Rules: Loop through each number $x$:
- Case 1 ($x > first$): A new maximum is found. Demote the old
firsttosecond(second = first), then updatefirst = x. - Case 2 ($x > second$ and $x < first$): $x$ is not the largest, but is strictly greater than
second. Updatesecond = x. - Case 3 ($x \le second$): Ignore $x$.
- Case 1 ($x > first$): A new maximum is found. Demote the old
- Distinct Constraint: The check
x < firstin Case 2 ensures duplicates of the maximum value do not improperly overwritesecond. - Result: If
secondremains-Infinity, no distinct second largest element exists; returnnull.
Why This Approach Works Best
Sorting takes $O(n \log n)$ time. Scanning twice (once for max, once for second max) takes $O(2n)$ time. This dual-variable method finds the answer in a single $O(n)$ pass using $O(1)$ memory.
Example Walkthrough
Input:
[12, 35, 1, 10, 34, 1]Unsorted array with distinct elements.
Execution Trace:
Check 12 (first=12) -> Check 35 (second=12, first=35) -> Check 1 -> Check 10 -> Check 34 (second=34) -> Check 1Tracks both top distinct values simultaneously.
Output:
3434 is the second largest distinct element.
Answer: Find second largest distinct element
function secondLargest(arr) {
let first = -Infinity;
let second = -Infinity;
for (const x of arr) {
if (x > first) {
second = first;
first = x;
} else if (x > second && x < first) {
second = x;
}
}
return second === -Infinity ? null : second;
}💡 Track the top two distinct maximum values simultaneously in one traversal.
Key Takeaways
- Demote
firsttosecondwhenever a new maximum is found. - Ensure duplicates of the max value do not overwrite
secondby checkingx < first. - Solves the problem in a single pass ($O(n)$ time) without extra space ($O(1)$ memory).