Replace elements by its rank in the array in JavaScript

In this problem, we are given an array, and we have to replace each element with its rank. The rank of an element is the position of the element when the array is arranged in sorted order with unique values only. In this article, we are going to learn about various approaches to replacing elements with their rank in an array using JavaScript.

Below are the examples to understand the problem clearly:

Example 1

Input: arr = [40, 10, 20, 30]
Output: [4, 1, 2, 3]

Explanation: The sorted, unique array is: [10, 20, 30, 40]. The ranks of elements are {10: 1, 20: 2, 30: 3, 40: 4}. When we replace elements with their rank, the array becomes: [4, 1, 2, 3].

Example 2

Input: arr = [100, 200, 100, 300]
Output: [1, 2, 1, 3]

Explanation: The sorted, unique array is: [100, 200, 300]. The ranks are {100: 1, 200: 2, 300: 3}. After replacement: [1, 2, 1, 3].

Using Sorting and Object Map

This is the most straightforward approach. We create a sorted array of unique elements, assign ranks using an object, then map the original array to their corresponding ranks.

Steps for Implementation

  • Create a copy of the array and remove duplicates using Set
  • Sort the unique elements in ascending order
  • Create an object to map each element to its rank
  • Replace elements in the original array with their ranks
function replaceWithRank(arr) {
    // Get unique elements and sort them
    let sortedArr = [...new Set(arr)].sort((a, b) => a - b);
    
    // Create rank mapping
    let rankMap = {};
    sortedArr.forEach((num, index) => {
        rankMap[num] = index + 1;
    });
    
    // Replace elements with their ranks
    return arr.map(num => rankMap[num]);
}

let arr1 = [40, 10, 20, 30];
console.log("Input:", arr1);
console.log("Output:", replaceWithRank(arr1));

let arr2 = [100, 200, 100, 300];
console.log("Input:", arr2);
console.log("Output:", replaceWithRank(arr2));
Input: [40, 10, 20, 30]
Output: [4, 1, 2, 3]
Input: [100, 200, 100, 300]
Output: [1, 2, 1, 3]

Using Sorting with Map Object

This approach uses JavaScript's Map object instead of a plain object. Map maintains insertion order and provides efficient key-value operations.

Steps for Implementation

  • Create unique sorted array
  • Use Map to store element-rank pairs
  • Map original array elements to their ranks
function replaceWithRankMap(arr) {
    // Get unique elements and sort them
    let sortedArr = [...new Set(arr)].sort((a, b) => a - b);
    
    // Create Map for rank mapping
    let rankMap = new Map();
    sortedArr.forEach((num, index) => {
        rankMap.set(num, index + 1);
    });
    
    // Replace elements with their ranks
    return arr.map(num => rankMap.get(num));
}

let arr3 = [5, 5, 1, 3, 1];
console.log("Input:", arr3);
console.log("Output:", replaceWithRankMap(arr3));
Input: [5, 5, 1, 3, 1]
Output: [3, 3, 1, 2, 1]

Comparison

Approach Time Complexity Space Complexity Advantage
Object Map O(N log N) O(N) Simple syntax, widely compatible
Map Object O(N log N) O(N) Better for non-string keys, maintains order

Key Points

  • Both approaches have the same time complexity due to sorting
  • Set is used to remove duplicates efficiently
  • Ranks start from 1 (not 0) for the smallest element
  • Duplicate elements in the original array get the same rank

Conclusion

Both methods effectively replace array elements with their ranks. The Object approach is more traditional, while Map offers better type safety and guaranteed insertion order. Choose based on your specific requirements and browser compatibility needs.

Updated on: 2026-03-15T23:19:01+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements