Single Number XOR Implementation in TypeScript
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 TypeScript as in every other language.
- ✓The runtime is identical to JavaScript: nums.reduce((a, b) => a ^ b, 0). What TypeScript adds is that the ^ operator is typed to reject mixing number and bigint, so the compiler catches the single most common porting bug at build time instead of at runtime.
- ✓Type the parameter readonly number[] rather than number[]: the function never mutates, and readonly widens the set of callers that can pass their arrays without a defensive copy.
- ✓The TypeScript trap: TypeScript types the result of ^ as number, which is a true statement about the JavaScript value and a misleading one about its RANGE: the type system has no 32-bit integer type, so it cannot warn you that ToInt32 truncated your input.
- ✓Types are erased, so runtime cost is exactly the JavaScript cost above. The one compile-time consideration is targeting: with an ES5 target and downlevelIteration disabled, spreading or for-of over non-array iterables emits slower helper code, so keep the input a real array and use reduce or an index loop rather than for-of over a Set.
What Single Number XOR actually is
Single Number XOR in TypeScript: what you actually reach for
TypeScript implementation
export function singleNumber(nums: readonly number[]): number {
return nums.reduce((acc, n) => acc ^ n, 0);
}
// The compiler catches the number/bigint mix that JS would run:
// const bad = 1n ^ 2; // TS2365: Operator '^' cannot be applied to bigint and number
export function singleNumberBig(nums: readonly bigint[]): bigint {
return nums.reduce((acc, n) => acc ^ n, 0n);
}
// A brand is the usual way to enforce the 32-bit range the type system lacks:
type Int32 = number & { readonly __int32: unique symbol };
const asInt32 = (n: number): Int32 => (n | 0) as Int32;
void asInt32;Think 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 TypeScript-specific trap
Performance in TypeScript, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which TypeScript type or library should I use for Single Number XOR?
The runtime is identical to JavaScript: nums.reduce((a, b) => a ^ b, 0). What TypeScript adds is that the ^ operator is typed to reject mixing number and bigint, so the compiler catches the single most common porting bug at build time instead of at runtime. Declare the signature as (nums: readonly number[]) => number to make the read-only contract explicit.
What is the most common TypeScript mistake when implementing Single Number XOR?
TypeScript types the result of ^ as number, which is a true statement about the JavaScript value and a misleading one about its RANGE: the type system has no 32-bit integer type, so it cannot warn you that ToInt32 truncated your input. A branded type such as type Int32 = number & { readonly __int32: unique symbol } is the usual workaround for enforcing the constraint at the boundary. The second trap is bigint: 1n ^ 2 is a compile error (Operator ^ cannot be applied to types bigint and number), which is helpful, but 1n ^ 2n silently gives you a bigint result that will not fit a number-typed field downstream.
How does Single Number XOR actually perform in TypeScript?
Types are erased, so runtime cost is exactly the JavaScript cost above. The one compile-time consideration is targeting: with an ES5 target and downlevelIteration disabled, spreading or for-of over non-array iterables emits slower helper code, so keep the input a real array and use reduce or an index loop rather than for-of over a Set. 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 trialMost AI and dev tools have a student plan. See which ones and how to claim them 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.