Third Maximum Number

Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.

Follow up: Can you find an O(n) solution?

Example 1
Inputnums = [3,2,1]
Output1
The first, second, and third distinct maximums are 3, 2, and 1, respectively.
Example 2
Inputnums = [1,2]
Output2
The third distinct maximum does not exist, so the maximum value 2 is returned instead.

Constraints

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

Asked at 8 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate