How to rank elements of an integer array based on their sorted order in JavaScript

1 Answer

0 votes
/*
    How to rank elements of an integer array based on their sorted order in JavaScript
    ---------------------------------------------------------------------------------

    Ranking rules:
    --------------
    - Lowest value gets rank 1
    - Equal values share the same rank
    - Rank increases only when encountering a new unique value

    Steps:
    1. Print the original array
    2. Create a copy of the array
    3. Sort the copy
    4. Build a map (value → rank)
    5. Apply ranks back to the original order
*/

// ------------------------------------------------------------
// Function 1: Print the array
// ------------------------------------------------------------
function printArray(arr) {
    console.log("Array:", arr);
}

// ------------------------------------------------------------
// Function 2: Create a copy of the array
// ------------------------------------------------------------
function copyArray(arr) {
    return [...arr]; // spread operator copy
}

// ------------------------------------------------------------
// Function 3: Sort the array copy
// ------------------------------------------------------------
function sortArray(arrCopy) {
    arrCopy.sort((a, b) => a - b); // ascending numeric sort
}

// ------------------------------------------------------------
// Function 4: Build a map of value → rank
// ------------------------------------------------------------
function buildRankMap(sortedArr) {

    const map = new Map();

    let rank = 1;
    let previous = sortedArr[0];

    map.set(previous, rank);

    for (let i = 1; i < sortedArr.length; i++) {
        if (sortedArr[i] !== previous) {
            rank++;
        }
        map.set(sortedArr[i], rank);
        previous = sortedArr[i];
    }

    return map;
}

// ------------------------------------------------------------
// Function 5: Apply ranks to original array order
// ------------------------------------------------------------
function applyRanks(original, map) {
    const ranked = new Array(original.length);

    for (let i = 0; i < original.length; i++) {
        ranked[i] = map.get(original[i]);
    }

    return ranked;
}

// ------------------------------------------------------------
// Main ranking function
// ------------------------------------------------------------
function rankArray(arr) {

    printArray(arr);

    if (!arr || arr.length === 0)
        return;

    // Step 1: Copy array
    const arrCopy = copyArray(arr);

    // Step 2: Sort the copy
    sortArray(arrCopy);

    // Step 3: Build rank map
    const rankMap = buildRankMap(arrCopy);

    // Step 4: Apply ranks to original order
    const ranked = applyRanks(arr, rankMap);

    console.log("Rank:", ranked);
}

// ------------------------------------------------------------
// MAIN PROGRAM
// ------------------------------------------------------------
const arr = [33, 99, 10, 25, 47, 11, 77];

rankArray(arr);


/*
run:

Array: [
  33, 99, 10, 25, 47, 11, 77
]
Rank: [
  4, 7, 1, 3, 5, 2, 6
]


*/

 



answered 12 hours ago by avibootz

Related questions

...