Command Palette
Search for a command to run...
Replace elements by its rank in the array
SORTING
Replace elements by its rank in the array
MediumArraySORTINGhash mapRanking
You are given an array of N integers.
Your task is to replace each element with its rank when the array is sorted in ascending order.
The smallest element gets rank 1.
The next smallest gets rank 2, and so on.
If two elements are equal, they share the same rank.
Example 1
Input
20 15 26 2 98 6Output
4 3 5 1 6 2Explanation
Sorted array:
2 6 15 20 26 98Ranks:
2 → 1
6 → 2
15 → 3
20 → 4
26 → 5
98 → 6
Final ranked array:
4 3 5 1 6 2Examples
Example 1
Input:
[20,15,26,2,98,6]
Output:
[4,3,5,1,6,2]
Loading...
[20,15,26,2,98,6]
[4,3,5,1,6,2]