Single Number XOR Implementation in Python
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Single Number XOR runs in O(n) time and O(1) space, in Python as in every other language.
- ✓There is no dedicated helper, and none is needed: functools.reduce(operator.xor, nums) folds the whole list, and operator.xor from the operator module is the function form of the ^ operator.
- ✓Reach for functools.reduce with operator.xor rather than writing your own loop when the fold IS the whole function, because reduce states the intent in one line.
- ✓The Python trap: The ^ operator is heavily overloaded in Python and two of the overloads bite here. On two sets it is SYMMETRIC DIFFERENCE, so if a helper hands you a set instead of a list, reduce silently returns a set rather than raising.
- ✓The XOR itself is one machine instruction, so the loop is entirely dominated by interpreter overhead: each iteration costs a bytecode dispatch, an unboxing of a PyLong, and possibly an allocation for the result object.
What Single Number XOR actually is
Single Number XOR in Python: what you actually reach for
Python implementation
from functools import reduce
from operator import xor
def single_number(nums: list[int]) -> int:
# 0 is the identity of XOR, so an empty list correctly yields 0.
return reduce(xor, nums, 0)
# Watch the overload: on sets, ^ is symmetric difference, not arithmetic.
assert single_number([4, 1, 2, 1, 2]) == 4
assert reduce(xor, [], 0) == 0
# Arbitrary precision: no 32-bit or 64-bit wrap-around exists here.
big = 1 << 200
assert single_number([big, 7, big]) == 7Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Python-specific trap
Performance in Python, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Python type or library should I use for Single Number XOR?
There is no dedicated helper, and none is needed: functools.reduce(operator.xor, nums) folds the whole list, and operator.xor from the operator module is the function form of the ^ operator. A plain for loop with an accumulator is just as idiomatic and marginally faster because it skips the per-item function call. If the data is already a NumPy array, numpy.bitwise_xor.reduce(arr) does the whole fold in compiled code.
What is the most common Python mistake when implementing Single Number XOR?
The ^ operator is heavily overloaded in Python and two of the overloads bite here. On two sets it is SYMMETRIC DIFFERENCE, so if a helper hands you a set instead of a list, reduce silently returns a set rather than raising. On two bools it returns a bool, because bool subclasses int. The third trap is that Python integers are arbitrary precision with conceptually infinite sign extension, so XOR-ing negative numbers gives mathematically correct two-complement results with no width limit, and a value that would wrap in every other language on this page does not wrap here.
How does Single Number XOR actually perform in Python?
The XOR itself is one machine instruction, so the loop is entirely dominated by interpreter overhead: each iteration costs a bytecode dispatch, an unboxing of a PyLong, and possibly an allocation for the result object. Small integers from -5 to 256 are interned so those iterations avoid the allocation, but a fold over large values allocates a new PyLong per step. On arrays of a million or more elements, numpy.bitwise_xor.reduce is typically one to two orders of magnitude faster than the pure-Python fold because it runs the whole reduction in C over a contiguous buffer. Time complexity is O(n) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialStudying for this? Replit free tiers and academic plans are laid out on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.