Description

Given an integer array nums, return the third distinct maximum number. If it doesn't exist, return the maximum.

Examples

Input:nums = [3,2,1]
Output:1
Explanation:

Third distinct max is 1.

Input:nums = [5,5,5,5]
Output:5
Explanation:

All elements are the same, so there is only one distinct value (5). Since the third distinct maximum does not exist, the maximum value 5 is returned.

Input:nums = [10,9,9,9,3,2,2,1,1]
Output:3
Explanation:

The distinct values are [10,9,3,2,1]. The first maximum is 10, second maximum is 9, and third maximum is 3.

Constraints

  • 1 ≤ nums.length ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!