Ranking array elements
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Ranking array elements means assigning each element a position relative to the others while preserving the connection to the original order. The main design choice is how to handle ties. Once that rule is clear, the implementation usually becomes a sort-plus-mapping problem.
What “Rank” Actually Means
For an array such as [40, 10, 20, 10], the sorted order is [10, 10, 20, 40]. But there are several legitimate ranking systems:
- dense rank:
1, 1, 2, 3 - competition rank:
1, 1, 3, 4 - average rank: ties get the average of their occupied positions
So before writing code, define the ranking rule. Otherwise two correct implementations may still disagree.
Dense Ranking by Sorting with Indexes
A common pattern is to sort pairs of (value, original_index), then walk the sorted data and assign ranks back to the original positions.
This works well because sorting puts equal values next to each other while the stored original index lets you rebuild the answer in input order.
Competition Ranking
If ties should skip rank numbers, use competition ranking instead.
This is common in leaderboards and sports standings.
If Values Are Unique, Ranking Is Simpler
When there are no duplicate values, you can rank by sorting once and building a lookup table.
This version is concise, but it assumes values are unique. With duplicates, the dictionary approach needs a tie policy.
Complexity and Tradeoffs
Most array-ranking implementations sort first, so the time complexity is typically O(n log n). That is usually the right trade for general input.
If the value range is tiny and known in advance, specialized counting-based approaches can do better, but that is a narrower problem than generic ranking.
The more important practical choice is usually not asymptotic speed. It is handling ties correctly and returning the answer in the shape the caller expects.
Ranking and Stability
If two equal values appear in the input, their relative order in the sorted intermediate form usually does not matter for ordinary ranking. But if the application attaches extra metadata to each element, stable sorting or explicit original indexes become important.
That is another reason the pair-based approach is a good default. It preserves the link back to the original array without guesswork.
A NumPy Version for Numeric Workflows
If the data is already in NumPy, you can still use the same core idea.
argsort gives the order of indices rather than direct ranks. From there, you still need to implement the tie policy explicitly. That is why understanding the ranking rule matters more than memorizing one library call.
Common Pitfalls
- Starting implementation before deciding how ties should be ranked.
- Losing the original positions after sorting.
- Using a simple dictionary-based rank map when duplicates are present.
- Confusing sorted order with the final rank array in original order.
- Optimizing away the sort before confirming ranking correctness.
Summary
- Ranking array elements is usually a sort-plus-mapping problem.
- The most important design choice is how ties should be handled.
- Sorting
(value, original_index)pairs is a reliable general pattern. - Unique-value arrays allow simpler lookup-table approaches.
- For most workloads, correctness of tie handling matters more than micro-optimizing the sort.
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.