19 · Sorting

#1636 Sort Array by Increasing Frequency

Easysolve on LeetCode ↗

frequently reported atAdobeAmazon

the problem

In plain words

Sort nums by how often each value appears (rarer first). If two values appear equally often, put the larger value first.

in nums = [1,1,2,2,2,3]

out [3,1,1,2,2,2]

in nums = [2,3,1,3,2]

out [1,3,3,2,2]

2 and 3 both appear twice → 3 first.

in nums = [-1,1,-6,4,5,-6,1,4,1]

out [5,-1,4,4,-6,-6,1,1,1]

  • 1 ≤ n ≤ 100
  • -100 ≤ nums[i] ≤ 100

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #347
    2. #179
      Largest Number
      Mediumin Sorting