Skip to the code
cf.
+10 XP

#202 Sort the Star Ratings

easy

A product page loads thousands of reviews, and each review has a star rating from 1 to 5. The ratings chart needs them sorted from lowest to highest.

Write sortRatings(ratings) that returns a new array with the ratings sorted ascending. Since there are only five possible values, aim for O(n) time.

Example 1

Input: ratings = [5,3,4,5,1,3]
Output: [1,3,3,4,5,5]

One 1, two 3s, one 4, and two 5s.

Example 2

Input: ratings = [2,2,2]
Output: [2,2,2]

All the same rating.

Constraints

  • 0 <= ratings.length <= 100000
  • 1 <= ratings[i] <= 5
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).