FunctionRacerProblemsRace

Kth Smallest Distinct Value

BeginnerSorting and searchingPython, JavaScript, TypeScript, Java, C++

Look only at the distinct values in nums, counting each value once however many times it appears. Return the k-th smallest of them, where k = 1 means the smallest.

k never exceeds the number of distinct values. In [4, 4, 1, 7] the distinct values are 1, 4 and 7, so k = 2 gives 4.

Example 1

nums [4,4,1,7]
k 2
returns 4

The distinct values in order are 1, 4, 7; the 2nd is 4.

Example 2

nums [5,3,5,3,9]
k 3
returns 9

The distinct values are 3, 5, 9; the 3rd is 9.

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 Sorting and searching problems