Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

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

1 Answer

0 votes
using System;
using System.Collections.Generic;

public class MyClass
{
    // ------------------------------------------------------------
    // Function 1: Print the array
    // ------------------------------------------------------------
    // Prints the contents of an integer array in a friendly format.
    public static void PrintArray(int[] arr)
    {
        Console.WriteLine("Array: [" + string.Join(", ", arr) + "]");
    }

    // ------------------------------------------------------------
    // Function 2: Create a copy of the array
    // ------------------------------------------------------------
    // Returns a new array with the same elements as the input.
    public static int[] CopyArray(int[] arr)
    {
        int[] copy = new int[arr.Length];
        Array.Copy(arr, copy, arr.Length);
        
        return copy;
    }

    // ------------------------------------------------------------
    // Function 3: Sort the array copy
    // ------------------------------------------------------------
    // Sorts the given array in ascending order (in-place).
    public static void SortArray(int[] arrCopy)
    {
        Array.Sort(arrCopy);
    }

    // ------------------------------------------------------------
    // Function 4: Build a map of value → rank
    // ------------------------------------------------------------
    // Ranking rules:
    // - Lowest value gets rank 1
    // - Equal values share the same rank
    // - Rank increases only when encountering a new unique value
    public static Dictionary<int, int> BuildRankMap(int[] sortedArr)
    {
        var map = new Dictionary<int, int>();

        int rank = 1;                    // First unique value gets rank 1
        int previous = sortedArr[0];     // Track last unique value

        map[previous] = rank;            // Assign rank to first element

        // Loop through sorted array and assign ranks
        for (int i = 1; i < sortedArr.Length; i++) {
            // If new unique value, increase rank
            if (sortedArr[i] != previous) {
                rank++;
            }

            map[sortedArr[i]] = rank;
            previous = sortedArr[i];
        }

        return map;
    }

    // ------------------------------------------------------------
    // Function 5: Apply ranks to original array order
    // ------------------------------------------------------------
    // Builds a new array where each element is replaced by its rank.
    public static int[] ApplyRanks(int[] original, Dictionary<int, int> map)
    {
        int[] ranked = new int[original.Length];

        for (int i = 0; i < original.Length; i++) {
            ranked[i] = map[original[i]];   // Replace value with rank
        }

        return ranked;
    }

    // ------------------------------------------------------------
    // Main ranking function
    // ------------------------------------------------------------
    // Orchestrates the whole process:
    // 1. Print original array
    // 2. Copy array
    // 3. Sort the copy
    // 4. Build rank map
    // 5. Apply ranks and print result
    public static void RankArray(int[] arr)
    {
        PrintArray(arr);

        if (arr == null || arr.Length == 0)
            return;

        // Step 1: Copy array
        int[] arrCopy = CopyArray(arr);

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

        // Step 3: Build rank map
        Dictionary<int, int> rankMap = BuildRankMap(arrCopy);

        // Step 4: Apply ranks to original order
        int[] ranked = ApplyRanks(arr, rankMap);

        Console.WriteLine("Rank:  [" + string.Join(", ", ranked) + "]");
    }

    // ------------------------------------------------------------
    // MAIN METHOD
    // ------------------------------------------------------------
    public static void Main(string[] args)
    {
        int[] 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 Jun 21 by avibootz

Related questions

...