Sort Array by Increasing Frequency - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,1,2,2,2,3]
›
Output:
[3,1,1,2,2,2]
💡 Note:
Frequencies: 1 appears 2 times, 2 appears 3 times, 3 appears 1 time. Sort by frequency: 3(1 time), 1(2 times), 2(3 times)
Example 2 — Same Frequencies
$
Input:
nums = [2,3,1,3,2]
›
Output:
[1,3,3,2,2]
💡 Note:
Frequencies: 1→1, 2→2, 3→2. Same frequency elements (2,3) sorted by value descending: 3 before 2
Example 3 — All Different
$
Input:
nums = [-1,1,-6,4,5,-6,1,4,1]
›
Output:
[5,-1,4,4,-6,-6,1,1,1]
💡 Note:
Frequencies: 5→1, -1→1, 4→2, -6→2, 1→3. Sort by frequency ascending, same frequency by value descending
Constraints
- 1 ≤ nums.length ≤ 100
- -100 ≤ nums[i] ≤ 100
Visualization
Tap to expand
Understanding the Visualization
1
Input Analysis
Count frequency of each element in the array
2
Sort Rules
Primary: frequency ascending, Secondary: value descending for ties
3
Output
Array sorted by the two-level criteria
Key Takeaway
🎯 Key Insight: Use two-level sorting - frequency first (ascending), then value (descending) for equal frequencies
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code