Sort Array by Increasing Frequency

Given an array of integers `nums`, sort the array in **increasing** order based on the frequency of the values. If multiple values have the same frequency, sort them in **decreasing** order. Return the sorted array.

30:00

Sort Array by Increasing Frequency
easy
Free written solution
Topics
Companies

Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.

Return the sorted array.

Example 1:
Input: {"nums":[1,1,2,2,2,3]}
Output: [3,1,1,2,2,2]
Constraints:
  • 1≤nums.length≤1001 \leq \text{nums.length} \leq 100

  • −100≤nums[i]≤100-100 \leq \text{nums}[i] \leq 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,1,2,2,2,3]}

The order depends on how often each value appears, so first count every value.

nums

1
1
2
2
2
3

count (0 distinct)

empty

Variables
VariableValue
n6
DepthFunction Call
Stack empty
0/13