Are All Occurrence Counts Unique
Problem
Given an array of integers, decide whether every distinct value occurs a different number of times. First build a frequency map; then check that the multiset of counts contains no duplicates.
Input
nums = [1, 2, 2, 1, 1, 3]Output
trueCounts are 1→3, 2→2, 3→1, all distinct.
def unique_counts(nums):
freq = {}
for x in nums:
freq[x] = freq.get(x, 0) + 1
seen = set()
for c in freq.values():
if c in seen:
return False
seen.add(c)
return True
function uniqueCounts(nums) {
const freq = new Map();
for (const x of nums) freq.set(x, (freq.get(x) || 0) + 1);
const seen = new Set();
for (const c of freq.values()) {
if (seen.has(c)) return false;
seen.add(c);
}
return true;
}
class Solution {
public boolean uniqueCounts(int[] nums) {
Map<Integer, Integer> freq = new HashMap<>();
for (int x : nums) freq.merge(x, 1, Integer::sum);
Set<Integer> seen = new HashSet<>();
for (int c : freq.values()) {
if (seen.contains(c)) return false;
seen.add(c);
}
return true;
}
}
bool uniqueCounts(vector<int>& nums) {
unordered_map<int, int> freq;
for (int x : nums) freq[x]++;
unordered_set<int> seen;
for (auto& p : freq) {
if (seen.count(p.second)) return false;
seen.insert(p.second);
}
return true;
}