Domain 1EasyCORE

Unique Number of Occurrences

The problem requires determining whether all elements in an array have unique frequencies of occurrence. The input is an array of integers, and the output should be a boolean value: `true` if each element's frequency is unique, otherwise `false`. Key constraints include: - Array length between 1 and 1000. - Elements range from -1000 to 1000.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#Hash Table

1207. Unique Number of Occurrences

Model: qwen2.5-coder:14b
Topics: Hash Table
FAANG asked: 1
Acceptance rate: 71.9%


Problem Breakdown

The problem requires determining whether all elements in an array have unique frequencies of occurrence. The input is an array of integers, and the output should be a boolean value: true if each element’s frequency is unique, otherwise false. Key constraints include:

  • Array length between 1 and 1000.
  • Elements range from -1000 to 1000.

Edge cases to consider:

  • An empty array (though the problem specifies a minimum length of 1).
  • Arrays with all identical elements.
  • Arrays with negative numbers.

Intuition

A brute-force approach would involve counting occurrences for each element and then checking if these counts are unique. This could be done using nested loops, but it would have a time complexity of O(n^2), which is inefficient given the constraints.

The key insight is to use a hash table (dictionary) to count occurrences in one pass (O(n)), and then check for uniqueness of these counts using another data structure that supports fast lookups, like a set. This approach reduces the overall time complexity to O(n).

Approach

  1. Count Occurrences: Traverse the array and use a dictionary to count how many times each element appears.
  2. Check Uniqueness: Use a set to track the counts of occurrences. If adding a count to the set fails (indicating it’s already present), return false.
  3. Return Result: If all counts are unique, return true.

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the array. We make two passes over the data: one for counting and one for checking uniqueness.
  • Space Complexity: O(n) in the worst case, where each element is unique.

Solution

def uniqueOccurrences(arr):
    # Step 1: Count occurrences of each element
    count_map = {}
    for num in arr:
        if num in count_map:
            count_map[num] += 1
        else:
            count_map[num] = 1
    
    # Step 2: Check if all counts are unique
    seen_counts = set()
    for count in count_map.values():
        if count in seen_counts:
            return False
        seen_counts.add(count)
    
    # Step 3: Return true if all counts are unique
    return True

Edge Cases Handled

  • All Identical Elements: The solution correctly identifies that all elements have the same frequency, thus returning true.
  • Negative Numbers: The use of a dictionary handles negative numbers naturally without any special handling.
  • Single Element Array: An array with one element trivially satisfies the condition, and the code returns true.

The provided solution is correct, efficient, and clear. It adheres to the problem constraints and handles all specified edge cases.