Given a list of integers, return a list of the same size where each position holds the dense rank of the value at that position. The largest value has rank 1, the next largest distinct value has rank 2, and so on with no gaps in the numbering. Equal values always share the same rank.

If the input list is null or empty, return an empty list.

Examples:

Input: nums = [40, 10, 20, 30] Output: [1, 4, 3, 2] Explanation: 40 is the largest so it ranks 1, then 30 ranks 2, 20 ranks 3, and 10 ranks 4 Input: nums = [10, 10, 20] Output: [2, 2, 1] Explanation: Both tens share rank 2 and there is no gap before or after them Input: nums = [-1, -5, -1, 0] Output: [2, 3, 2, 1] Explanation: The distinct values from largest to smallest are 0, -1 and -5 Input: nums = [5] Output: [1] Explanation: A single value is always rank 1 Input: nums = [] Output: [] Explanation: An empty list produces an empty list
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay November 2026 (Nov 16–30)Sponsored by FLXBL

Contest runs November 16 - 30. Complete challenges to climb the leaderboard!

Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In