Skip to main content

1207. Unique Number of Occurrences

easy

Count each value's frequency, then ask whether all those frequencies are distinct. The whole problem is two hash structures stitched together: a counting map and a set built from its values.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Given an array of integers arr, return true if the number of occurrences of each value in the array is unique, or false otherwise.

Constraints

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

Examples

Example 1

Input
arr = [1,2,2,1,1,3]
Output
true

Explanation: 1 occurs 3 times, 2 occurs 2 times, 3 occurs 1 time — three distinct counts.

Example 2

Input
arr = [1,2]
Output
false

Explanation: Both 1 and 2 occur once, so the counts collide.

Example 3

Input
arr = [-3,0,1,-3,1,1,1,-3,10,0]
Output
true

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Hints

Progressive — try the first before opening the next.

Hint 1

Pass one: build a map of value -> count.

Hint 2

Pass two: put every count into a set. If the set's size equals the map's size, every count was unique.

Hint 3

Equivalently, len(set(counter.values())) == len(counter) is the one-liner.

Solution approach

Reveal approach

Two-phase hash structure. Phase one is a running-frequency map (value -> count) built in a single pass. Phase two collects map.values() into a set; if set size equals map size, every count is distinct and the answer is true. Both phases are O(n) and combine cleanly into one expression. The set is what closes the problem — without it you'd be doing pairwise comparison of counts in O(k^2).

Complexity

Time
O(n)
Space
O(n)

Related patterns

  • hash-map
  • hash-set
  • counting

Related problems

Asked at

Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).

  • Amazon
  • Microsoft

Practice these live with InterviewChamp.AI

Drill Unique Number of Occurrences and Hash Tables problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →