Single Number
Find the one number that appears once when every other number appears twice
DSA, Easy, Bit Manipulation
Which structure or algorithm, and what does it cost.
A lab logs whole-number measurements. Each measurement is sent twice for safety, but one backup copy got lost. No two measurements have the same value, so one value appears once and every other value appears exactly twice. The list nums holds the log in no set order. Return the value that appears once.
Examples
Constraints
- 1 <= length of
nums<= 25,001, and the length is odd. - Each value is a whole number between -500,000 and 500,000.
- Exactly one value appears once. Every other value appears exactly twice.
Your answer must contain
- The simple method and its cost; a method that meets the target cost and why it is correct; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n) time and O(1) memory.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Look at the pairs, not at the single value. Ask what combining two equal numbers could leave behind.
Hint 2 the approach
Counting how often each value appears works, but it needs memory. Look for a bit operation under which a pair cancels itself.
Hint 3 the key step
Xor of a number with itself is 0, and the order of xor does not matter. What is left if you combine the whole list that way?
Walkthrough
Full walkthrough brute force to best
Brute force
For each value, count how many times it appears in the whole list, and return the value whose count is 1. Each count scans the full list, so this is O(n^2) time and O(1) extra memory. With 25,001 values that is about 625 million comparisons.
Better method
Walk through the list with a set. If a value is not in the set, add it. If it is already there, remove it. At the end the set holds the one value. This is O(n) time, but the set can grow to about half the list, so it needs O(n) extra memory.
Best method
Combine all values with xor, which gives a 1 in each bit where the two inputs differ. Two equal numbers give 0, xor with 0 leaves a number unchanged, and the order does not matter. So every pair cancels wherever it sits in the list, and what is left is the value without a partner. This needs one running value, so memory is O(1).
Hand trace
Run on [9, 4, 9, 7, 7, 4, 12]. The running value starts at 0. Xor 9 (1001): the value is 1001. Xor 4 (100): 1101, which is 13. Xor 9: 100, which is 4, so the first pair is gone. Xor 7 (111): 11, which is 3. Xor 7: 100, which is 4. Xor 4: 0, so every pair has cancelled. Xor 12 (1100): 1100, which is 12. Return 12.
Code
def single_number(nums):
result = 0
for value in nums:
result ^= value
return resultTime and memory
O(n) time, because it makes one pass. O(1) memory, because it keeps one running value.
Common mistakes
- Using + or - instead of xor. Sums do not cancel pairs, so the single value is lost in the total.
- Using a set or a count table and then calling the memory constant. Both grow with the list.
- Assuming the values are positive. Xor works on negative values too: with [-6, 15, 15] the result is -6.