Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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] Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.
Example 2:
Input: nums = [2,3,1,3,2] Output: [1,3,3,2,2] Explanation: '2' and '3' both have a frequency of 2, so they are sorted in decreasing order.
Example 3:
Input: nums = [-1,1,-6,4,5,-6,1,4,1] Output: [5,-1,4,4,-6,-6,1,1,1]
Constraints:
1 <= nums.length <= 100-100 <= nums[i] <= 100Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,1,2,2,2,3]
[2,3,1,3,2]
[-1,1,-6,4,5,-6,1,4,1]
sort-characters-by-frequency)divide-array-into-equal-pairs)most-frequent-number-following-key-in-an-array)maximum-number-of-pairs-in-array)node-with-highest-edge-score)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1636: Sort Array by Increasing Frequency
class Solution {
public int[] frequencySort(int[] nums) {
int[] cnt = new int[201];
List<Integer> t = new ArrayList<>();
for (int v : nums) {
v += 100;
++cnt[v];
t.add(v);
}
t.sort((a, b) -> cnt[a] == cnt[b] ? b - a : cnt[a] - cnt[b]);
int[] ans = new int[nums.length];
int i = 0;
for (int v : t) {
ans[i++] = v - 100;
}
return ans;
}
}
// Accepted solution for LeetCode #1636: Sort Array by Increasing Frequency
func frequencySort(nums []int) []int {
cnt := make([]int, 201)
for _, v := range nums {
cnt[v+100]++
}
sort.Slice(nums, func(i, j int) bool {
a, b := nums[i]+100, nums[j]+100
return cnt[a] < cnt[b] || cnt[a] == cnt[b] && a > b
})
return nums
}
# Accepted solution for LeetCode #1636: Sort Array by Increasing Frequency
class Solution:
def frequencySort(self, nums: List[int]) -> List[int]:
cnt = Counter(nums)
return sorted(nums, key=lambda x: (cnt[x], -x))
// Accepted solution for LeetCode #1636: Sort Array by Increasing Frequency
use std::collections::HashMap;
impl Solution {
pub fn frequency_sort(mut nums: Vec<i32>) -> Vec<i32> {
let n = nums.len();
let mut map = HashMap::new();
for &num in nums.iter() {
*map.entry(num).or_insert(0) += 1;
}
nums.sort_by(|a, b| {
if map.get(a) == map.get(b) {
return b.cmp(a);
}
map.get(a).cmp(&map.get(b))
});
nums
}
}
// Accepted solution for LeetCode #1636: Sort Array by Increasing Frequency
function frequencySort(nums: number[]): number[] {
const map = new Map<number, number>();
for (const num of nums) {
map.set(num, (map.get(num) ?? 0) + 1);
}
return nums.sort((a, b) => map.get(a) - map.get(b) || b - a);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.