Skip to content
Onsite

Counting Sort

EasyP3Coding

Sort an array of non-negative integers in ascending order using counting sort and return the sorted array.

Counting sort does not compare elements. It counts how many times each value occurs and then writes the values back in order, which is fast when the range of values is small.

Example 1

Input
nums = [4,2,2,8,3,3,1]
Output
[1,2,2,3,3,4,8]

Example 2

Input
nums = [0,0,0]
Output
[0,0,0]

Example 3

Input
nums = []
Output
[]

Constraints

  • 0 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^4

Hints

Run checks the 3 visible tests, Submit all 9.