Given an array of integers arr
, determine whether the number of occurrences of each value in the array is unique. Return true
if the occurrences are unique, and false
otherwise.
For example:
arr = [1, 2, 2, 1, 1, 3]
should return true
because 1 occurs 3 times, 2 occurs 2 times, and 3 occurs 1 time. These counts (3, 2, 1) are unique.arr = [1, 2]
should return false
because 1 occurs 1 time and 2 occurs 1 time. The counts are not unique.arr = [-3, 0, 1, -3, 1, 1, 1, -3, 10, 0]
should return true
because -3 occurs 3 times, 0 occurs 2 times, 1 occurs 4 times, and 10 occurs 1 time. These counts (3, 2, 4, 1) are unique.Explain how you would approach this problem, considering the time and space complexity of your solution. Discuss any potential edge cases and provide code implementation in Python. The code should be well-commented and easy to understand.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The core idea is to count how many times each number appears in the list. After counting, we check if the counts themselves are all different. If they are, then the answer is yes; otherwise, it's no.
Here's how the algorithm would work step-by-step:
def unique_occurrences_brute_force(numbers):
frequency_list = []
for number in numbers:
count = 0
for other_number in numbers:
if number == other_number:
count += 1
frequency_list.append(count)
# Convert frequency list to set to remove duplicate counts for accurate iteration
unique_numbers = list(set(numbers))
frequency_of_numbers = []
for number in unique_numbers:
count = 0
for element in numbers:
if number == element:
count += 1
frequency_of_numbers.append(count)
# We must check each frequency against all other frequencies for uniqueness
for i in range(len(frequency_of_numbers)):
for j in range(i + 1, len(frequency_of_numbers)):
# If duplicate frequencies are found, return false
if frequency_of_numbers[i] == frequency_of_numbers[j]:
return False
return True
The goal is to determine if each number in a list appears a unique number of times. The most efficient way is to first count how many times each number appears and then check if those counts are all different.
Here's how the algorithm would work step-by-step:
def unique_occurrences(number_list):
number_counts = {}
for number in number_list:
number_counts[number] = number_counts.get(number, 0) + 1
# Use a set to efficiently check for duplicate counts
counts_seen = set()
for number in number_counts:
count_of_number = number_counts[number]
# If the current count already exists, there are duplicate occurrences.
if count_of_number in counts_seen:
return False
counts_seen.add(count_of_number)
# All occurrences are unique
return True
Case | How to Handle |
---|---|
Null or empty input array | Return true since an empty array trivially satisfies the condition of unique occurrences. |
Array with a single element | Return true as a single element has a unique occurrence count of 1. |
Array with all elements being the same value | The occurrence count will be equal to the array's size; check if the frequency map already contains that number. |
Array with maximum integer values | Using a HashMap or frequency array can still accommodate large integer values, given sufficient memory. |
Integer overflow in frequency counts | Use a data type large enough to accommodate potential maximum frequency (e.g., long in Java) or check for overflows. |
Large input array size (scalability) | A HashMap approach provides O(n) time complexity, which scales reasonably well. |
Negative numbers in the input array | HashMaps can handle negative numbers as keys without any special treatment. |
Extreme difference in frequency counts between elements | This should not cause any issues as we iterate through the frequency map once and use a set to ensure uniqueness, so the solution is independent of frequency count value. |