You are given a non-empty array of integers nums. Every integer appears twice except for one.
Return the integer that appears only once.
You must implement a solution with runtime complexity and use only extra space.
Example 1:
Input: nums = [3,2,3]
Output: 2Example 2:
Input: nums = [7,6,6,7,8]
Output: 8Constraints:
1 <= nums.length <= 10000-10000 <= nums[i] <= 10000
You should aim for a solution with O(n) time and O(1) space, where n is the size of the input array.
A brute force approach would iterate through the array, checking each element using a nested loop. If a duplicate is found, we continue to the next element; otherwise, the current element is the required number. This results in an O(n^2) solution. Can you think of a better way? Maybe a data structure can help detect duplicates efficiently.
We use a hash set, iterating through the array and adding elements that are not in the set while removing those that are already present. After the iteration, the required number remains in the hash set. This results in an O(n) space solution. Can you further optimize it? Maybe a bitwise operator could be useful here.
Think about the bitwise XOR ("^"). What is the result when two identical numbers are XORed?
When two identical numbers are XORed, they cancel out, resulting in zero. Since every number appears twice except for one, the XOR of the entire array gives the number that appears only once.
Before attempting this problem, you should be comfortable with:
We are given an array where every element appears twice except one, and we need to find that unique element.
The brute force idea is straightforward:
This approach is simple and easy to understand, especially for beginners, because it directly follows the problem statement without using extra data structures or clever tricks.
i in the array:nums[i] is unique (flag = true).j:i != j and nums[i] == nums[j]:flag = false and stop checkingflag is still true, return nums[i]We are given an array where every number appears exactly twice except one, and we need to find that single number.
A convenient way to solve this is by using a hash set to track numbers as we iterate:
Because:
At the end, the set will contain only one element, which is the answer.
seen.num in the array:num is already in seen:We are given an array where every element appears exactly twice except one, and we need to find that unique element.
Sorting helps simplify the problem:
So we can scan the array in steps of two:
nums[i] == nums[i + 1], they form a valid pair → skip bothnums[i] must be the unique elementThis approach avoids extra space and relies on the structure created by sorting.
nums.i = 0.i < len(nums) - 1:nums[i] == nums[i + 1]:i += 2nums[i] (this is the single number)nums[i]We are given an array where every number appears exactly twice except one, and we need to find that unique number.
This problem is a perfect fit for bit manipulation, specifically the XOR (^) operation.
Key properties of XOR:
a ^ a = 0 (a number XORed with itself cancels out)a ^ 0 = a (XOR with 0 keeps the number unchanged)Because of these properties:
res = 0.res = res ^ numres contains the single numberres.The optimal solution relies on understanding that a ^ a = 0 and a ^ 0 = a. Candidates unfamiliar with bitwise operations often resort to hash sets or sorting, missing the elegant space solution. Memorizing these XOR properties is essential for bit manipulation problems.
A common mistake is implementing a hash map or set to track occurrences when the problem explicitly asks for extra space. While these approaches work functionally, they fail to meet the space constraint and miss the intended learning objective of the problem.