JavaScript Array Ranking



In this article, we will learn to rank elements in arrays using JavaScript. Ranking elements in an array is common in various applications, such as sorting scores, evaluating performance, or analyzing data.

What is Array Ranking?

Array ranking involves assigning a rank to each element of an array based on its value, where the largest value gets the highest rank and the smallest value gets the lowest rank. This ranking can be extended to multiple arrays to analyze and compare data across different datasets.

Problem Statement

We are given multiple arrays of numbers. The task is to compute ranks for the elements in each array, where the rank indicates the position of a number in descending order within its respective array.

Input 

const array1 = [10,23,53,74,33,56,6,0,43,45,11];
const array2 = [52,46,27,28,4,11,53,6,75,75,22];
const array3 = [26,18,10,12,31,12,5,8,44,34,65];

Output

[ [3,2,1,1,1,1,2,3,2,2,3],
[1,1,2,2,2,2,1,2,1,1,2],
[2,3,3,3,3,3,3,1,2,3,1],]

Using map() and reduce()

This approach calculates ranks for multiple arrays and returns the ranks in the same structure as the input arrays. 

Step to rank an array in JavaScript

Following are the steps to rank an array in JavaScript using map() and reduce() ?

  • Ranking Individual Arrays:
    • Arrays are sorted in descending order.
    • Ranks are assigned using a Map, with ties sharing the same rank.
  • Combining Results:
    • Arrays are transposed to align ranks element-wise, ranked, and transposed back.  

Array.prototype.map(): Creates a new array by applying a provided function to each element of the array. Used to map ranks to original elements ? 

arr.map(value => rankMap.get(value))

Array.prototype.sort(): Sorts an array in-place. Sorting is based on a comparison function provided (descending or ascending order) ?

arr.sort((a, b) => b - a)

Array.prototype.reduce(): Reduces an array to a single value or structure (like a Map) by applying a function cumulatively ?

.reduce((r, m, v) => ...)

Example

Below is an example of ranking an array in JavaScript using map() and reduce() ?

const array1 = [10,23,53,74,33,56,6,0,43,45,11];
const array2 = [52,46,27,28,4,11,53,6,75,75,22];
const array3 = [26,18,10,12,31,12,5,8,44,34,65];
const transpose = (rank, arr) => {
   return arr.map((el, ind) => {
      return [...(rank[ind] || []), el];
   });
};
const ranks = arr => {
   return arr.map(
      Map.prototype.get,
      [...arr]
      .sort((a, b) => b - a)
      .reduce((r => (m, v) => m.set(v, (r++, m.get(v)) || r))(0), new Map)
   );
};
const findRanks = (...arrs) => {
   return arrs
   .reduce(transpose, [])
   .map(ranks)
   .reduce(transpose, []);
};
console.log(findRanks(array1, array2, array3));

Output

[ [3,2,1,1,1,1,2,3,2,2,3],
[1,1,2,2,2,2,1,2,1,1,2],
[2,3,3,3,3,3,3,1,2,3,1],]

Time Complexity: O(n?log?(n)+n?m), where n is the length of each array and m is the number of arrays. Sorting takes O(n?log?(n)), and mapping ranks takes O(n?m).
Space Complexity: O(n+m), storing sorted arrays, ranks, and intermediate results.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-01-22T00:46:49+05:30

678 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements