InterviewPrepKit

Home / Coding / Arrays & Hashing

Unique Number of Occurrences

easy Original β†—
Solving tips
  • Recognize two composed sub-problems: count each value's frequency, then check that the frequencies have no duplicates.
  • Build a Counter, then return len(freq) == len(set(freq.values())) β€” deduplicating the frequencies shrinks the set exactly when two values collide on a count. O(n) time, O(n) space.
  • Alternative duplicate check: sort the frequency list and compare adjacent pairs (useful without cheap hash sets).
  • Pitfall: deduplicate the frequencies (freq.values()), not the values themselves; compare against len(freq), not len(arr).

Problem

Given an integer array arr, decide whether every distinct value appears a different number of times. Return True if no two distinct values share the same frequency, False otherwise.

Examples

  • arr = [1, 2, 2, 1, 1, 3] β†’ True β€” 1 appears 3 times, 2 appears twice, 3 appears once: frequencies {3, 2, 1} are all different.
  • arr = [1, 2] β†’ False β€” both values appear exactly once.
  • arr = [-3, 0, 1, -3, 1, 1, 8, 8, -3, 4] β†’ False β€” both -3 and 1 appear 3 times, so two distinct values collide on the same frequency.

Constraints

  • 1 <= arr.length <= 1000
  • -1000 <= arr[i] <= 1000

The bounds are tiny β€” even a quadratic solution passes β€” but the clean answer is a single linear counting pass.

Think about it first

Hint 1 The problem is really two sub-problems: first compute the frequency of each distinct value, then check a property of those frequencies.
Hint 2 "No two frequencies are equal" is the same as saying the collection of frequencies contains no duplicates. How do you detect duplicates in one line?
Hint 3 Build a `Counter` of the array, then compare `len(counter.values())` with the size of `set(counter.values())` β€” if deduplicating shrinks the collection, two values shared a frequency.
Your workspace Not runnable by design β€” this is your interview scratchpad. Saved on this device.