Dense Rank
MediumData EngineeringArray
Description
Given an array of numbers, return the dense rank of each element: the smallest value has rank 1, the next distinct value rank 2, and so on, with equal values sharing a rank and no gaps.
Examples
Input:
[10,20,20,30]Output:
[1,2,2,3]Explanation:
Distinct values are ranked from smallest upward, with equal values sharing a rank and no numbers skipped.
Input:
[3,1,2]Output:
[3,1,2]Explanation:
Distinct values are ranked from smallest upward, with equal values sharing a rank and no numbers skipped.
Input:
[5,5,5]Output:
[1,1,1]Explanation:
Distinct values are ranked from smallest upward, with equal values sharing a rank and no numbers skipped.
Constraints
- •
1 ≤ length ≤ 10⁴