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 arr, return true if the number of occurrences of each value in the array is unique or false otherwise.
Example 1:
Input: arr = [1,2,2,1,1,3] Output: true Explanation: The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences.
Example 2:
Input: arr = [1,2] Output: false
Example 3:
Input: arr = [-3,0,1,-3,1,1,1,-3,10,0] Output: true
Constraints:
1 <= arr.length <= 1000-1000 <= arr[i] <= 1000Problem summary: Given an array of integers arr, return true if the number of occurrences of each value in the array is unique or false otherwise.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,2,1,1,3]
[1,2]
[-3,0,1,-3,1,1,1,-3,10,0]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1207: Unique Number of Occurrences
class Solution {
public boolean uniqueOccurrences(int[] arr) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : arr) {
cnt.merge(x, 1, Integer::sum);
}
return new HashSet<>(cnt.values()).size() == cnt.size();
}
}
// Accepted solution for LeetCode #1207: Unique Number of Occurrences
func uniqueOccurrences(arr []int) bool {
cnt := map[int]int{}
for _, x := range arr {
cnt[x]++
}
vis := map[int]bool{}
for _, v := range cnt {
if vis[v] {
return false
}
vis[v] = true
}
return true
}
# Accepted solution for LeetCode #1207: Unique Number of Occurrences
class Solution:
def uniqueOccurrences(self, arr: List[int]) -> bool:
cnt = Counter(arr)
return len(set(cnt.values())) == len(cnt)
// Accepted solution for LeetCode #1207: Unique Number of Occurrences
struct Solution;
use std::collections::HashMap;
use std::collections::HashSet;
impl Solution {
fn unique_occurrences(arr: Vec<i32>) -> bool {
let mut hm: HashMap<i32, i32> = HashMap::new();
for x in arr {
*hm.entry(x).or_default() += 1;
}
let mut hs = HashSet::new();
for x in hm.values() {
if !hs.insert(x) {
return false;
}
}
true
}
}
#[test]
fn test() {
let arr = vec![1, 2, 2, 1, 1, 3];
assert_eq!(Solution::unique_occurrences(arr), true);
let arr = vec![1, 2];
assert_eq!(Solution::unique_occurrences(arr), false);
let arr = vec![-3, 0, 1, -3, 1, 1, 1, -3, 10, 0];
assert_eq!(Solution::unique_occurrences(arr), true);
}
// Accepted solution for LeetCode #1207: Unique Number of Occurrences
function uniqueOccurrences(arr: number[]): boolean {
const cnt: Map<number, number> = new Map();
for (const x of arr) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
return cnt.size === new Set(cnt.values()).size;
}
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.