We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Counting Sort 1
  2. Discussions

Counting Sort 1

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 190 Discussions, By:

votes

Please Login in order to post a comment

  • packet_racket
    8 months ago+ 5 comments

    Is anyone else seeing an error in sample test case 1? It expects a '0' as the last element of the result. This implies that the test case expects the algorithm to count one additional element beyond the largest element in the list.

    input: 63 54 17 78 43 70 32 97 16 94 74 18 60 61 35 83 13 56 75 52 70 12 24 37 17 0 16 64 34 81 82 24 69 2 30 61 83 37 97 16 70 53 0 61 12 17 97 67 33 30 49 70 11 40 67 94 84 60 35 58 19 81 16 14 68 46 42 81 75 87 13 84 33 34 14 96 7 59 17 98 79 47 71 75 8 27 73 66 64 12 29 35 80 78 80 6 5 24 49 82

    my result: 2 0 1 0 0 1 1 1 1 0 0 1 3 2 2 0 4 4 1 1 0 0 0 0 3 0 0 1 0 1 2 0 1 2 2 3 0 2 0 0 1 0 1 1 0 0 1 1 0 2 0 0 1 1 1 0 1 0 1 1 2 3 0 1 2 0 1 2 1 1 4 1 0 1 1 3 0 0 2 1 2 3 2 2 2 0 0 1 0 0 0 0 0 0 2 0 1 3 1

    expected result: 2 0 1 0 0 1 1 1 1 0 0 1 3 2 2 0 4 4 1 1 0 0 0 0 3 0 0 1 0 1 2 0 1 2 2 3 0 2 0 0 1 0 1 1 0 0 1 1 0 2 0 0 1 1 1 0 1 0 1 1 2 3 0 1 2 0 1 2 1 1 4 1 0 1 1 3 0 0 2 1 2 3 2 2 2 0 0 1 0 0 0 0 0 0 2 0 1 3 1 0

    26|
    Permalink
    View more Comments..
  • sknoop
    8 months ago+ 2 comments

    javascript

    function countingSort(arr) {
        let counterArray = Array(100).fill(0);
        
        for (let number of arr) {
            counterArray[number]++
        }
        return counterArray;
    }
    
    17|
    Permalink
  • bmacho
    8 months ago+ 6 comments

    Python

    def countingSort(arr):
        
        sol=[0]*100  # creates an array filled with 0s
        for a in arr : 
            sol[a] += 1
    
        return sol
    
    10|
    Permalink
    View more Comments..
  • shani_dagan_115
    9 months ago+ 0 comments

    C# code:

    public static List<int> countingSort(List<int> arr)
        {
            int[] occurences = new int[100];
            foreach (var item in arr)
                occurences[item]++;
    
            return occurences.ToList();
        }
    
    8|
    Permalink
  • carlosiazzetti
    7 months ago+ 0 comments

    Easy Java solution:

    public static List<Integer> countingSort(List<Integer> arr) {
        //creates list with 100 elements filled with zeros
        List<Integer> res = new ArrayList<Integer>(Collections.nCopies(100, 0));
        for(Integer a : arr)
                res.set(a, res.get(a)+1);
        return res;
    }
    
    5|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature