Replace each number with its rank among the distinct values in the array.
Given an integer array, assign a rank to every element based on its value relative to the other distinct values in the array.
Return a new array where each position contains the rank of the original value at that position.
arr.ranked of the same length as arr.ranked[i] is the rank of arr[i] among the distinct values in arr.1 <= arr.lengthExample 1
Input
arr = [40, 10, 20, 30]
Output
[4, 1, 2, 3]
Explanation
The distinct values in sorted order are [10, 20, 30, 40]. Their ranks are 1, 2, 3, and 4 respectively.
Example 2
Input
arr = [100, 100, 100]
Output
[1, 1, 1]
Explanation
All elements are equal, so they all receive the same smallest rank.
Example 3
Input
arr = [37, 12, 28, 9, 100, 56, 80, 5, 12]
Output
[5, 3, 4, 2, 8, 6, 7, 1, 3]
Explanation
After sorting the distinct values, each number is replaced by its 1-indexed position among the unique values.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.