Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Sort Array by Increasing Frequency

XPChallenge Points: 10
levelLevel: Easy

You are given an integer array nums. Your task is to rearrange the numbers in a specific way:

  • First, numbers that appear less frequently should come first.

  • If two numbers have the same frequency, the number with the larger value should appear earlier.

After applying these rules, return the newly arranged array.

Example 1:

Input: nums = [4, 4, 1, 2, 2, 3]

Output: [3,1,2,2,4,4]

Explanation:

Frequencies → 3→1, 1→1, 2→2, 4→2 Lowest freq first → 3, 1 For same freq (2 & 4), higher value comes first → 4,4 then 2,2.

Example 2:

Input: nums = [7, 7, 8, 8, 8, 6]

Output: [6,7,7,8,8,8]

Explanation:

6 occurs 1 time 7 occurs 2 times 8 occurs 3 times Sorted by frequency → 6 → 7 → 8

Example 3:

Input: nums = [9, -2, -2, 5, 9, 5, 5]

Output: [-2, -2, 9, 9, 5, 5, 5]

Explanation:

Freq(-2)=2, Freq(9)=2 → same freq ⇒ sort by value desc → 9 first Freq(5)=3 comes last

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result