Sort Array By Increasing Frequency

Given an integer array, sort it by increasing frequency of each value, and if two values have the same frequency, sort them in decreasing numerical order. This is a classic frequency-aware comparator problem.

Input Format

nums = array of integers

Output Format

array sorted by increasing frequency

Constraints

  • 1 <= nums.length <= 10^5; -100 <= nums[i] <= 100

Examples

Example 1:

Input:

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

Output:

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

Explanation:

3 appears once, 1 appears twice, and 2 appears three times.

Example 2:

Input:

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

Output:

[1,3,3,2,2]

Explanation:

Tie-breaking uses the larger value first when frequencies are equal.

Loading...
Sort Array By Increasing Frequency