Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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
[ [6, 9, 2, 1, 7, 3, 10, 11, 5, 4, 8], [3, 4, 7, 6, 10, 9, 2, 11, 1, 1, 8], [7, 9, 10, 8, 5, 8, 11, 10, 2, 4, 1] ]
Using Simple Ranking Method
This approach ranks each array individually by comparing each element with all other elements in the same array.
Example: Basic Array Ranking
function rankArray(arr) {
return arr.map(value => {
// Count how many elements are greater than current value
let rank = 1;
for (let i = 0; i value) {
rank++;
}
}
return rank;
});
}
const scores = [85, 92, 78, 95, 88];
console.log("Original array:", scores);
console.log("Ranks:", rankArray(scores));
Original array: [85, 92, 78, 95, 88] Ranks: [3, 2, 5, 1, 4]
Using map() and reduce() for Multiple Arrays
This approach calculates ranks for multiple arrays and returns the ranks in the same structure as the input arrays.
Steps to Rank Multiple Arrays
-
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
Key Methods Used
Array.prototype.map(): Creates a new array by applying a function to each element:
arr.map(value => rankMap.get(value))
Array.prototype.sort(): Sorts an array based on a comparison function:
arr.sort((a, b) => b - a) // Descending order
Array.prototype.reduce(): Reduces an array to a single value or structure:
.reduce((accumulator, current, index) => { /* logic */ })
Example: Advanced Ranking with Multiple Arrays
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("Multiple array ranking:");
console.log(findRanks(array1, array2, array3));
Multiple array ranking: [ [6, 9, 2, 1, 7, 3, 10, 11, 5, 4, 8], [3, 4, 7, 6, 10, 9, 2, 11, 1, 1, 8], [7, 9, 10, 8, 5, 8, 11, 10, 2, 4, 1] ]
Handling Ties in Rankings
When elements have the same value, they should receive the same rank:
function rankWithTies(arr) {
const sorted = [...arr].sort((a, b) => b - a);
const rankMap = new Map();
let currentRank = 1;
for (let i = 0; i rankMap.get(value));
}
const scoresWithTies = [90, 85, 90, 78, 85];
console.log("Scores:", scoresWithTies);
console.log("Ranks with ties:", rankWithTies(scoresWithTies));
Scores: [90, 85, 90, 78, 85] Ranks with ties: [1, 3, 1, 5, 3]
Complexity Analysis
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.
Conclusion
Array ranking in JavaScript can be implemented using various approaches, from simple comparison methods to advanced techniques using map() and reduce(). Choose the method that best fits your specific use case and performance requirements.
