AlgoMaster Logo

Third Maximum Number

easyFrequency5 min readUpdated June 23, 2026

Understanding the Problem

This problem asks for the third largest distinct value in the array. The word "distinct" carries the weight here. For the array [2, 2, 3, 1], the three distinct maximums are 3, 2, and 1, not 3, 2, and 2. Duplicates do not count.

There is also a fallback rule: if fewer than three distinct values exist, return the overall maximum. So arrays like [1, 1] (one distinct value) and [1, 2] (two distinct values) both return their largest element.

The task reduces to tracking the top three distinct values during a scan of the array.

Key Constraints:

  • 1 <= nums.length <= 10^4 → With n up to 10,000, O(n log n) sorting runs comfortably, and even O(n^2) would pass. The follow-up asks for O(n), which points toward a single-pass solution.
  • -2^31 <= nums[i] <= 2^31 - 1 → Full 32-bit integer range, which means Integer.MIN_VALUE (-2^31) is itself a valid input. An approach that initializes its tracking variables to Integer.MIN_VALUE as a sentinel cannot distinguish a real -2^31 from "not yet set." Uninitialized state needs a value outside the 32-bit range.

Approach 1: Sorting

Intuition

Sort the array, then scan from the largest element toward the smallest, counting distinct values as you pass them. The third distinct value reached is the answer. If fewer than three distinct values exist, the count never reaches three, and the largest element is returned instead.

Sorting groups equal values into adjacent runs, so a value differs from a distinct maximum exactly when it differs from its neighbor in the sorted order. That makes the distinct count a single comparison per element.

Algorithm

  1. Sort the array in ascending order.
  2. Start from the end (largest element) and walk backward.
  3. Count distinct values as you go. Each time you see a value different from the previous one, increment a counter.
  4. When the counter reaches 3, return that value.
  5. If you reach the beginning of the array without finding 3 distinct values, return the largest element (last element after sorting).

Example Walkthrough

1Initial unsorted array
0
2
1
2
2
3
3
1
1/6

Code

Sorting does O(n log n) work to surface three values. The next approach tracks only those three values in a single pass, dropping the cost to O(n).

Approach 2: Track Three Maximums (Optimal)

Intuition

Only three values matter: the first, second, and third distinct maximum. Instead of sorting, maintain three variables and update them during a single scan. For each element, find where it fits among the current top three and shift the lower values down.

Two edge cases need care. Duplicates: if the current element equals any of the three maximums, skip it so the same value cannot occupy two slots. Initialization: since -2^31 is a valid input, it cannot serve as a "not yet set" sentinel. Use Long.MIN_VALUE (or the equivalent) for an uninitialized slot, since no 32-bit integer can equal a 64-bit minimum.

Algorithm

  1. Initialize three variables first, second, third to Long.MIN_VALUE (or equivalent sentinel that's outside the 32-bit integer range).
  2. For each number in the array:
    • If it equals first, second, or third, skip it (duplicate).
    • If it's greater than first, shift: third = second, second = first, first = num.
    • Else if it's greater than second, shift: third = second, second = num.
    • Else if it's greater than third, set third = num.
  3. After the scan, if third is still Long.MIN_VALUE (never assigned a real value), return first (the maximum). Otherwise, return third.

Example Walkthrough

1Initialize: first=-INF, second=-INF, third=-INF
0
2
i
1
2
2
3
3
1
1/6

Code