Problem · Array

Rank Transform an Array

Learn this problem
EasyAgoda logoAgodaFULLTIMEONSITE INTERVIEW

Problem statement

Given an integer array values, replace every value with its rank.

  • The smallest distinct value has rank 1.
  • Larger distinct values receive consecutive larger ranks.
  • Equal values receive the same rank.

Return the transformed array in the original element order.

Function

arrayRankTransform(values: int[]) → int[]

Examples

Example 1

values = [40,10,20,30]return = [4,1,2,3]

The ascending distinct values are 10, 20, 30, 40, with ranks 1, 2, 3, 4.

Example 2

values = [100,100,100]return = [1,1,1]

There is only one distinct value, so every position receives rank 1.

Constraints

  • 1 <= values.length <= 10^5
  • -10^9 <= values[i] <= 10^9

More Agoda problems

drafts saved locally
public int[] arrayRankTransform(int[] values) {
    // Write your solution here.
}
values[40,10,20,30]
expected[4,1,2,3]
checking account