Imagine you're analyzing data from a survey where almost every response appears exactly three times in your dataset, but there's one special response that appears only once. Your task is to efficiently identify this unique response.
Given an integer array nums where every element appears three times except for one element which appears exactly once, find and return the single element.
Challenge: Your solution must run in O(n) time and use only O(1) extra space - no sorting allowed!
Example: In array [2,2,3,2], the number 3 appears once while 2 appears three times, so return 3.
Input & Output
Visualization
Time & Space Complexity
Single pass to build hash map + single pass to find answer
Hash map stores at most n/3 unique elements in worst case
Constraints
- 1 โค nums.length โค 3 ร 104
- -231 โค nums[i] โค 231 - 1
- Each element in the array appears exactly three times except for one element which appears exactly once