Imagine you have a collection of items where every item appears exactly twice, except for one special item that appears only once. Your mission is to find that single unique item efficiently!
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
Challenge: You must implement a solution with linear runtime complexity O(n) and use only constant extra space O(1).
Example: In the array [2, 2, 1], the number 1 appears only once while 2 appears twice. So the answer is 1.
Input & Output
Visualization
Time & Space Complexity
Single pass through the array with O(1) hash set operations
Hash set can store up to n/2 + 1 elements in the worst case
Constraints
- 1 โค nums.length โค 3 ร 104
- -3 ร 104 โค nums[i] โค 3 ร 104
- Each element in the array appears twice except for one element which appears only once