AlgoMaster Logo

Second Largest Element

Last Updated: June 7, 2026

easy
4 min read

Understanding the Problem

The word distinct is what makes this more than a sorting exercise. In [5, 5, 4], the largest value is 5, and the second largest distinct value is 4, not the other 5. Repeated copies of the maximum are still the maximum, so they cannot fill the second slot.

With fewer than two distinct values, there is no second largest to return. That happens when every element is equal, as in [5, 5, 5], or when the array has a single element, as in [7]. In both cases the answer is -1.

The values can be negative and span the full 32-bit signed range, which affects how you initialize the trackers. Starting them at 0 would break on all-negative inputs like [-5, -2, -9], where the second largest is -5. Use a sentinel smaller than any possible input, such as Long.MIN_VALUE or negative infinity, then check at the end whether the second tracker ever moved off that sentinel.

Key Constraints:

  • Second largest means second largest distinct value → In [5, 5, 4] the answer is 4, not 5. Equal copies of the maximum never count as the second largest, so the logic must skip values that equal the current largest.
  • Return -1 when there is no second distinct value → Arrays like [5, 5, 5] and [7] have only one distinct value, so there is no second largest. Detect this case and return -1.
  • -2^31 <= nums[i] <= 2^31 - 1 → Values can be negative and cover the full signed range. Never seed the trackers with 0. Use a sentinel like Long.MIN_VALUE or negative infinity so even all-negative inputs are handled correctly.

Approach 1: Two Pass

Intuition

Split the work into two passes. The first pass finds the maximum. The second scans again for the largest value strictly less than that maximum. The strict "less than" keeps duplicates of the maximum out of the answer. If the second pass finds nothing below the maximum, every element equals the maximum, and the answer is -1.

Algorithm

  1. Scan the array once to find maxVal, the largest value.
  2. Initialize second to a sentinel smaller than any input, such as negative infinity.
  3. Scan the array again. For each value, if it is strictly less than maxVal and greater than second, update second.
  4. If second is still the sentinel, no value was below the maximum, so return -1.
  5. Otherwise, return second.

Example Walkthrough

Input:

0
5
1
5
2
4
nums

The first pass finds the maximum. The largest value across 5, 5, and 4 is 5, so maxVal = 5.

The second pass looks for the largest value strictly below 5. Start with second at negative infinity. The first 5 equals maxVal, so skip it. The second 5 also equals maxVal, so skip it. The 4 is less than 5 and greater than negative infinity, so update second = 4.

Since second moved off the sentinel, the result is 4.

4
result

Code

The two passes are clear to read, but they walk the array twice. With a little more care in the comparison logic, the same result can be found in a single scan.

Approach 2: One Pass

Intuition

Instead of finding the maximum first and scanning again, track both the largest and the second largest at the same time. As you walk the array, each value falls into one of three cases. Larger than the current largest: the old largest becomes the new second largest. Between the largest and the second largest: it becomes the new second largest. Too small: ignore it.

A value equal to the current largest is skipped entirely, so a second copy of the maximum never reaches the second slot. At the end, if the second tracker never moved off its sentinel, there was no distinct value below the maximum, so the answer is -1.

Algorithm

  1. Initialize largest and second to a sentinel smaller than any input, such as negative infinity.
  2. For each value x in the array:
    • If x > largest, set second = largest and then largest = x.
    • Else if x < largest and x > second, set second = x.
    • If x equals largest, skip it so duplicates do not affect second.
  3. If second is still the sentinel, return -1.
  4. Otherwise, return second.

Example Walkthrough

Input:

0
3
1
9
2
5
nums

Start with both largest and second at negative infinity.

Read 3. It is greater than largest, so the old largest shifts down to second, leaving second at negative infinity, and largest becomes 3.

Read 9. It is greater than largest, so second becomes the old largest of 3, and largest becomes 9.

Read 5. It is less than largest of 9 and greater than second of 3, so second becomes 5.

At the end, largest = 9 and second = 5. Since second moved off the sentinel, the result is 5.

5
second

Code