Unique Number of Occurrences - Complete Solution Guide
Unique Number of Occurrences is LeetCode problem 1207, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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] <= 1000
Detailed Explanation
This problem presents a unique twist on frequency counting: we're not just tasked with counting how many times each number appears, but rather with determining if those *counts* are themselves unique. Essentially, you need to answer whether it's possible for two distinct numbers in the input array `arr` to have occurred the exact same number of times. For example, if `arr = [1,2,2,1,1,3]`, we first tally the occurrences: '1' appears 3 times, '2' appears 2 times, and '3' appears 1 time. The set of these occurrence counts is `{3, 2, 1}`. Since all values within this set are distinct, the function should return `true`.
Solution Approach
The provided solution elegantly breaks down this problem into two distinct, highly efficient stages, leveraging the power of hash-based data structures. The first stage is dedicated to accurately tallying the occurrences of every number in the input array `arr`. It iterates through `arr` once, using a hash map (like Python's `dict` named `counts`) where each unique number from `arr` becomes a key, and its total frequency within `arr` is its corresponding value. This pass efficiently builds a complete frequency profile of `arr` in O(N) average time. Once the `counts` map is populated, the second stage begins its crucial task: verifying if these collected frequencies (the values from the `counts` map) are themselves unique. A hash set (like Python's `set` named `occurrences`) is employed here. The solution iterates through the *values* of the `counts` map (which are the frequencies). For each frequency encountered, it attempts to add it to the `occurrences` set. The key insight of using a hash set is its O(1) average-time complexity for both insertion and membership testing. If a frequency is encountered that is *already present* in the `occurrences` set, it immediately signifies that we have found two different numbers that occurred the same number of times, allowing the function to promptly return `false`. If the loop completes without finding any duplicate frequencies in `occurrences`, it confirms that all occurrence counts are unique, and `true` is returned.
Step-by-Step Algorithm
- Step 1: Initialize an empty hash map (or dictionary) to store the counts of each number in the input array.
- Step 2: Iterate through the input array. For each number, increment its count in the hash map (using `getOrDefault` or a similar method to handle cases where the number is not yet in the map).
- Step 3: Initialize an empty set.
- Step 4: Iterate through the values (counts) of the hash map. For each count:
- Step 5: Check if the count is already present in the set. If it is, return `false` (non-unique frequencies).
- Step 6: If the count is not in the set, add it to the set.
- Step 7: If the loop completes without returning `false`, return `true` (unique frequencies).
Key Insights
- **Two-Stage Frequency Analysis:** The problem's core lies in a two-tier counting process: first, determine the frequency of each element in the input array; then, critically, verify the uniqueness of *those resulting frequencies*. This clear separation into initial counting and subsequent uniqueness checking is fundamental.
- **Efficient Initial Counting with Hash Map:** Employing a hash map (like Python's `dict`) for the first pass to tally element frequencies in `arr` is crucial. It allows for O(1) average-time updates for each element, leading to an overall O(N) average time complexity for processing the entire input array, significantly more efficient than sorting for counting.
- **Leveraging Hash Set for Uniqueness Validation:** The second phase, which checks the uniqueness of the *frequencies themselves*, is made highly efficient by using a hash set (like Python's `set`). Attempting to add each frequency to the set provides O(1) average-time detection of duplicates. If a frequency is already present in the set, we immediately know the condition is violated, enabling an early and efficient exit without needing to process further counts or sort them.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table.
Companies
Asked at: Datadog.