Counting Sort 1

Sort by

recency

|

262 Discussions

|

  • + 0 comments

    This question shouldnt ask you return a fixed length array, that is not necessary and leads to unnecessary bugs

    This question annoyed me a lot even though I was implementing correct logic.

  • + 0 comments

    in JS:

    function countingSort(arr) {
        let countingArray = new Array(100).fill(0);
        for (let num of arr) {
            countingArray[num]++;
        }
        return countingArray;
    }
    
  • + 0 comments

    vector countingSort(vector arr) {

    vector<int> vect(100);
    
    for(int& a : arr){
        vect[a]++;
    }
    return vect;
    

    }

  • + 0 comments
    from collections import Counter
    
    def countingSort(arr):
        return [dict(Counter(arr)).get(i, 0) for i in range(100)]
    
  • + 0 comments

    C#:

    public static List<int> countingSort(List<int> arr)
        {
            // Initialize a frequency List<int> with 100 elements, all set to 0
            List<int> result = new List<int>(new int[100]);
            
            // Iterate through the input list and count the occurrences
            foreach (int num in arr)
            {
                result[num]++;
            }
            
            // Return the frequency list
            return result;
        }