Count Integers That Appear Exactly Once
AgodaData Engineer · Technical ScreenLast updated
Count distinct integers with frequency exactly one using complete occurrence counts, including empty arrays and separated duplicates.
Given an array of integers, count how many distinct numbers occur exactly once.
Function Contract
Implement count_single_occurrences(numbers) -> int.
A number contributes one to the answer if its frequency in the entire array is exactly one.
Constraints and Clarifications
These numeric bounds are explicit practice assumptions.
0 <= len(numbers) <= 200000.- Each value is an integer from
-1000000000through1000000000. - Repeated values do not contribute, regardless of where their occurrences appear.
- Return
0for an empty array. - Aim for expected
O(n)time.
Examples
numbers = [4, 2, 1, 4, 4, 1, 3]
Output: 2
Only 2 and 3 appear once.
numbers = [5, 5, -1, -1]
Output: 0
Hints
Hint 1
The first occurrence is not necessarily the only occurrence: Determine the final frequency before deciding whether a value contributes to the answer.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.