Magicsheet logo

Unique Number of Occurrences

Easy
46.9%
Updated 6/1/2025

Unique Number of Occurrences

What is this problem about?

The Unique Number of Occurrences interview question asks you to check if the frequency of each element in an array is unique. For example, if '1' appears 3 times and '2' appears 2 times, the occurrences are unique (3 and 2). However, if '1' appears 3 times and '2' also appears 3 times, the occurrences are not unique.

Why is this asked in interviews?

This Unique Number of Occurrences coding problem is a frequent warm-up question at companies like Apple, Uber, and Adobe. It tests your ability to use two-step data processing: first counting frequencies and then checking those counts for duplicates. It’s a great way to verify that a candidate understands how to use multiple Hash Tables or Sets in conjunction.

Algorithmic pattern used

The standard Array, Hash Table interview pattern for this problem involves two collections. First, you use a Hash Map (or frequency array) to count how many times each number appears in the input. Second, you take all the values (the counts) from that Map and insert them into a Hash Set. If the size of the Set is equal to the number of entries in the Map, it means all counts were unique.

Example explanation

Suppose the array is [1, 2, 2, 1, 1, 3].

  1. Count occurrences:
    • 1 appears 3 times.
    • 2 appears 2 times.
    • 3 appears 1 time.
  2. The counts are [3, 2, 1].
  3. Add counts to a Set: {3, 2, 1}.
  4. The Set has 3 elements, and there were 3 distinct numbers. Everything is unique. If the array was [1, 2], both have a count of 1. The Set would only have one element {1}, so you would return false.

Common mistakes candidates make

Candidates sometimes forget to check the counts and instead check the numbers themselves. Another mistake is trying to sort the array and count in a single pass without a second data structure, which can be done but is often more bug-prone than the simple two-step Map-and-Set approach.

Interview preparation tip

Get comfortable with "Map of counts" logic. It is the building block for dozens of interview problems. Practice transitioning from a Map's keys to its values, as this problem specifically focuses on the properties of the values.

Similar Questions