Third Maximum Number
Look at the distinct values in nums. Return the third largest of them. If there are fewer than three distinct values, return the largest value instead.
Example 1
nums [3,2,1]
returns 1
The third largest is 1.
Example 2
nums [2,2,3,1]
returns 1
Distinct values are 3, 2, 1, so the third largest is 1.
Example 3
nums [1,2]
returns 2
Only two distinct values, so return the largest: 2.
Constraints
1 <= len(nums) <= 50-100 <= nums[i] <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Arrays problems
- Times in the LeadEasy
- Valid Mountain ArrayEasy
- Best Rotation ScoreMedium
- Best Stretch Around a RingMedium
- Fewest Swaps to SortMedium
- Insert IntervalMedium
- Longest HillMedium
- Maximum SubarrayMedium