Hacker Rank Counting Sort Node.js

Megan
1 min readFeb 10, 2022

Break Down

In this challenge, users are given an array to sort. Typically sorting algorithms are based on comparison, meaning the arrays are sorted by comparing each element.

An alternative sorting method is the counting sort. Instead of comparing elements, you create an array of integers whose index range covers the entire range of values in your array to sort.

Each time a value occurs in the initial array, you increment the counter at that index. At the end, run through your counting array, printing the value of each non-zero valued index that number of times.

Basically, you will be given an array of numbers from 0–100 and you need to count how many times those numbers appear in the array and return the count.

Make an empty array for the counts.

The first for loop makes 100 elements with the value of 0, because any number in the array will be less than 100.

Then, the second for loop adds an increment of 1 for every time the value appears.

Return the array of counts.

--

--