Two Non Repeating Elements in Array Using XOR in DSA Typescript - Time & Space Complexity
We want to understand how fast the XOR method finds two unique numbers in an array where all others repeat.
How does the time needed grow as the array gets bigger?
Analyze the time complexity of the following code snippet.
function findTwoNonRepeating(arr: number[]): [number, number] {
let xor = 0;
for (const num of arr) {
xor ^= num;
}
let setBit = xor & -xor;
let x = 0, y = 0;
for (const num of arr) {
if ((num & setBit) !== 0) x ^= num;
else y ^= num;
}
return [x, y];
}
This code finds two numbers that appear only once in an array where every other number appears twice, using XOR operations.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two separate loops over the array.
- How many times: Each loop runs once over all elements, so twice in total.
As the array size grows, the number of operations grows roughly twice the size of the array.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 XOR checks |
| 100 | About 200 XOR checks |
| 1000 | About 2000 XOR checks |
Pattern observation: Operations grow linearly with input size, doubling because of two passes.
Time Complexity: O(n)
This means the time to find the two unique numbers grows directly in proportion to the array size.
[X] Wrong: "Because there are two loops, the time complexity is O(n²)."
[OK] Correct: The loops run one after another, not nested, so their times add up, not multiply.
Understanding this XOR approach shows you can solve problems efficiently without extra space, a skill valued in interviews.
"What if the array had three non-repeating elements instead of two? How would the time complexity change?"
Practice
Solution
Step 1: Understand XOR properties
XOR of two same numbers is 0, and XOR with 0 is the number itself.Step 2: Apply XOR on array elements
XORing all elements cancels out repeating ones, leaving XOR of two unique elements.Final Answer:
To cancel out all repeating elements and isolate the unique ones -> Option CQuick Check:
XOR cancels duplicates = isolate unique elements [OK]
- Thinking XOR sorts the array
- Using XOR to count frequencies
- Assuming XOR reverses elements
Solution
Step 1: Identify initial XOR value
XOR with 0 returns the number itself, so start with 0.Step 2: Confirm initialization
Initializing xor as 0 allows proper XOR accumulation over array elements.Final Answer:
let xor = 0; -> Option BQuick Check:
Start XOR with 0 for correct accumulation [OK]
- Starting XOR with 1 changes results
- Using null or undefined causes errors
- Starting XOR with -1 gives wrong output
const arr = [2, 3, 7, 9, 2, 3];
let xor = 0;
for (const num of arr) {
xor ^= num;
}
console.log(xor);Solution
Step 1: XOR all elements in array
Calculate XOR stepwise: 0^2=2, 2^3=1, 1^7=6, 6^9=15, 15^2=13, 13^3=14.Step 2: Final XOR value
The final XOR after all elements is 14.Final Answer:
14 -> Option DQuick Check:
XOR all elements = 14 [OK]
- Stopping XOR early
- Incorrect XOR calculation order
- Confusing XOR with addition
const arr = [4, 1, 2, 1, 2, 5];
let xor = 0;
for (let i = 0; i <= arr.length; i++) {
xor ^= arr[i];
}
console.log(xor);Solution
Step 1: Check loop boundary
Loop uses i <= arr.length which causes out-of-bounds access at last iteration.Step 2: Correct loop condition
Change to i < arr.length to avoid accessing undefined element.Final Answer:
Loop condition should be i < arr.length, not i <= arr.length -> Option AQuick Check:
Loop index must be less than array length [OK]
- Using wrong loop boundary causing runtime error
- Confusing XOR with AND operator
- Initializing xor incorrectly
[6, 1, 3, 6, 1, 5], which two numbers are the non-repeating elements found using XOR method?Solution
Step 1: XOR all elements
6^1=7, 7^3=4, 4^6=2, 2^1=3, 3^5=6 (final XOR = 6).Step 2: Find rightmost set bit of XOR
Rightmost set bit of 6 (binary 110) is 2 (binary 010).Step 3: Divide elements into two groups and XOR separately
Group 1 (bit set): 6, 3, 6 -> XOR = 3; Group 2 (bit not set): 1, 1, 5 -> XOR = 5.Final Answer:
[3, 5] -> Option AQuick Check:
Two unique elements = [3, 5] [OK]
- Not separating elements by set bit
- Mixing order of unique elements
- Incorrect XOR calculations
Ready to prove it?
Pass the test and this pattern moves unknown
