FunctionRacerProblemsRace

Third Maximum Number

EasyArraysPython, JavaScript, TypeScript, Java, C++

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Arrays problems