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.
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.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.
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).
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.
The invariant is that first >= second >= third always holds and the three variables are the largest distinct values seen so far. Testing a new value from the top down (greater than first? than second? than third?) places it in the highest slot it qualifies for, and the cascading shift (third = second, second = first, first = num) pushes the displaced values down by one rank, which preserves the ordering.
The duplicate check is what keeps the three slots distinct. Without it, an input like [1, 1, 1] would shift 1 into all three slots and return 1 as the third maximum even though only one distinct value exists.
first, second, third to Long.MIN_VALUE (or equivalent sentinel that's outside the 32-bit integer range).first, second, or third, skip it (duplicate).first, shift: third = second, second = first, first = num.second, shift: third = second, second = num.third, set third = num.third is still Long.MIN_VALUE (never assigned a real value), return first (the maximum). Otherwise, return third.