Counting Sort 1

Sort by

recency

|

692 Discussions

|

  • + 0 comments

    Scala solution

    def countingSort(arr: Array[Int]): Array[Int] = {
        // Write your code here
            val frequency = Array.fill(100)(0)
            for (num <- arr) {
            frequency(num) += 1
            }
            frequency
        }
    
  • + 0 comments
     int totalCount = arr.Count;
    
     List<int> a = new List<int>(totalCount);
     for(int i =0;i< 100;i++)
     {
         a.Add(0);
     }
     int count = 0;
     for (int i = 0; i < arr.Count; i++)
     {
         if (a[arr[i]] > 0)
         {
             a[arr[i]] =++a[arr[i]];
         }
         else
         {
             a[arr[i]] = ++count;
         }
         count = 0;
     }
    
  • + 0 comments

    why its not working for 5th test case def countingSort(arr): arr.sort() d=arr[-1] l=[] for i in range(d+1): count=0 for j in arr: if i==j: count+=1 l.append(count) return l

  • + 0 comments

    C#

    public static List<int> countingSort(List<int> arr)
        {
            var res = new int[100];      
            
            foreach (var item in arr) {
                res[item] += 1;
            }
            
            return res.ToList();
        }
    
  • + 0 comments

    mine python solution

    def CountingSort(arr):
        lps = [0]*(max(arr)+1)
        for i in arr:
            lps[i] += 1
        return lps
    

    correct solution according to hacker rank

    def CountingSort(arr):
        lps = [0]*(100)
        for i in arr:
            lps[i] += 1
        return lps