FunctionRacerProblemsRace

Rank Transform of an Array

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

Replace each item of arr by its rank: the smallest distinct value has rank 1, the next smallest distinct value has rank 2, and so on. Equal values share the same rank.

Return the list of ranks in the original order.

Example 1

arr [40,10,20,30]
returns [4,1,2,3]

10 is rank 1, 20 rank 2, 30 rank 3, 40 rank 4.

Example 2

arr [100,100,100]
returns [1,1,1]

All equal, so all rank 1.

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