Skip to the code
cf.
+10 XP

#93 Rank Values for a Color Scale

easy

A choropleth map colors regions by rank instead of raw value, so a few huge outliers don't wash out the colors. The region values are in values.

Write colorLevels(values) that replaces each value with its level: the smallest distinct value gets level 1, the next distinct value level 2, and so on. Equal values get the same level.

Example 1

Input: values = [40,10,40,25]
Output: [3,1,3,2]

The distinct values in order are 10, 25, 40, so they get levels 1, 2, 3.

Example 2

Input: values = []
Output: []

No regions, no levels.

Constraints

  • 0 <= values.length <= 10^5
  • -10^9 <= values[i] <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).