Third Maximum Number

Given an array, return the third distinct maximum number. Partial sorting or a small fixed set of tracked maxima is enough, so a full sort is unnecessary.

Input Format

nums = array of integers

Output Format

third distinct maximum or maximum if fewer than three distinct values exist

Constraints

  • 1 <= nums.length <= 10^4; -2^31 <= nums[i] <= 2^31 - 1

Examples

Example 1:

Input:

nums = [3,2,1]

Output:

1

Explanation:

The third distinct maximum is 1.

Example 2:

Input:

nums = [1,2]

Output:

2

Explanation:

Fewer than three distinct values means return the maximum.

Loading...
Third Maximum Number