Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Rank Transform of an Array

XPChallenge Points: 10
levelLevel: Easy

You are given an array of integers arr. Your task is to replace every value with its ranking relative to the other values in the array.
The ranking rules are:

  • Rank starts at 1 for the smallest number.

  • If two values are equal, they must receive the same rank.

  • Larger numbers should always have a larger rank value.

  • Ranks must be assigned in the most compact way, without skipping numbers.

Your job is to return the transformed array where each element is replaced by its assigned rank.

Example 1:

Input: arr = [12, 5, 5, 20]

Output: [3, 1, 1, 4]

Explanation:

Sorted unique values → [5, 12, 20] 5 → rank 1, 12 → rank 2, 20 → rank 3 Duplicate 5 gets the same rank.

Example 2:

Input: arr = [7, 15, 7, 30, 15]

Output: [1, 2, 1, 3, 2]

Example 3:

Input: arr = [50]

Output: [1]

Explanation:

Only one element → always rank 1.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result