Single Number XOR Implementation in JavaScript
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 JavaScript as in every other language.
- ✓nums.reduce((a, b) => a ^ b, 0) is the whole implementation, using the built-in Array.prototype.reduce.
- ✓Always pass the explicit 0 seed to reduce. Without it, reduce on an empty array throws TypeError: Reduce of empty array with no initial value, which is a crash rather than the mathematically correct 0.
- ✓The JavaScript trap: This is the one language on the page where the operator changes your data before it runs. JavaScript numbers are IEEE-754 doubles, but every bitwise operator first applies ToInt32, which truncates to a signed 32-bit integer.
- ✓On a monomorphic array of small integers, V8 compiles the reduce down to an unboxed 32-bit XOR loop and the cost approaches native.
What Single Number XOR actually is
Single Number XOR in JavaScript: what you actually reach for
JavaScript implementation
function singleNumber(nums) {
// The 0 seed matters: reduce on [] with no seed THROWS.
return nums.reduce((a, b) => a ^ b, 0);
}
console.log(singleNumber([4, 1, 2, 1, 2])); // 4
console.log(singleNumber([])); // 0
// ToInt32 runs BEFORE the XOR, this is the trap:
console.log(2 ** 31 ^ 0); // -2147483648, not 2147483648
// Above 32 bits you must switch to BigInt:
const big = [1n << 40n, 7n, 1n << 40n];
console.log(big.reduce((a, b) => a ^ b, 0n)); // 7nThink 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 JavaScript-specific trap
Performance in JavaScript, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which JavaScript type or library should I use for Single Number XOR?
nums.reduce((a, b) => a ^ b, 0) is the whole implementation, using the built-in Array.prototype.reduce. There is no bitwise helper module. For values that exceed 32 bits you must leave Number entirely and use BigInt, whose ^ operator is arbitrary precision: nums.map(BigInt).reduce((a, b) => a ^ b, 0n).
What is the most common JavaScript mistake when implementing Single Number XOR?
This is the one language on the page where the operator changes your data before it runs. JavaScript numbers are IEEE-754 doubles, but every bitwise operator first applies ToInt32, which truncates to a signed 32-bit integer. So 2 ** 31 ^ 0 evaluates to -2147483648, and any value at or above 2 ** 31, or any fractional value, is silently mangled before the XOR happens. Integers above Number.MAX_SAFE_INTEGER lose precision earlier still. If your identifiers can exceed 2^31 - 1 (database IDs routinely do), this algorithm returns a confidently wrong answer with no error.
How does Single Number XOR actually perform in JavaScript?
On a monomorphic array of small integers, V8 compiles the reduce down to an unboxed 32-bit XOR loop and the cost approaches native. Two things break that: a callback that the engine cannot inline, and an array whose elements have mixed types (holes, undefined, doubles), which forces boxed generic elements. A plain for loop over an Int32Array is the fastest form and avoids the per-element callback entirely. BigInt XOR is dramatically slower, each operation heap-allocates a new arbitrary-precision value. 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 trialBefore paying full price for a tool, check the student deals on AI and developer tools listed 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.