Find the Only Non Repeating Element Using XOR in DSA Typescript - Time & Space Complexity
We want to know how fast we can find the single element that does not repeat in a list using XOR.
How does the time needed grow as the list gets bigger?
Analyze the time complexity of the following code snippet.
function findNonRepeating(arr: number[]): number {
let result = 0;
for (const num of arr) {
result ^= num;
}
return result;
}
This code finds the only number that appears once while all others appear twice by using XOR.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: One loop that goes through each number once.
- How many times: Exactly once for each element in the array.
As the list grows, the number of XOR operations grows directly with the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 XOR operations |
| 100 | 100 XOR operations |
| 1000 | 1000 XOR operations |
Pattern observation: The operations increase in a straight line as input size increases.
Time Complexity: O(n)
This means the time to find the unique element grows directly with the number of elements in the list.
[X] Wrong: "Using XOR means we have to compare every element with every other element, so it is slow like O(n²)."
[OK] Correct: XOR is applied in a single pass without nested loops, so it only needs one operation per element, making it much faster.
Understanding this XOR trick shows you can solve problems efficiently by using clever operations instead of brute force. This skill is valuable in many coding challenges.
"What if the array contains more than one non-repeating element? How would the time complexity change?"
Practice
Solution
Step 1: Understand XOR property for identical numbers
XOR of a number with itself always results in 0 because bits cancel out.Step 2: Apply XOR to example
For example, 5 XOR 5 = 0.Final Answer:
It returns 0 -> Option CQuick Check:
X XOR X = 0 [OK]
- Thinking XOR returns the number itself
- Confusing XOR with OR or AND
- Assuming XOR sums the numbers
Solution
Step 1: Identify correct initial value for XOR
For XOR operations, starting with 0 is correct because 0 XOR any number returns that number.Step 2: Check code snippets
Onlylet result = 0;correctly initializes for XOR accumulation.Final Answer:
let result = 0; -> Option BQuick Check:
Initial XOR value = 0 [OK]
- Starting XOR with 1 instead of 0
- Using null or undefined which cause errors
- Not initializing the variable
const arr = [2, 3, 2, 4, 3];
let result = 0;
for (const num of arr) {
result ^= num;
}
console.log(result);Solution
Step 1: XOR all elements in the array
Calculate stepwise: 0 XOR 2 = 2, 2 XOR 3 = 1, 1 XOR 2 = 3, 3 XOR 4 = 7, 7 XOR 3 = 4.Step 2: Identify the non-repeating element
All repeating numbers cancel out, leaving 4 as the only non-repeating element.Final Answer:
4 -> Option DQuick Check:
Result after XOR = 4 [OK]
- Not XORing all elements
- Confusing output with last element
- Assuming sum instead of XOR
const arr = [1, 2, 1, 3, 2];
let result;
for (let i = 0; i < arr.length; i++) {
result ^= arr[i];
}
console.log(result);Solution
Step 1: Check variable initialization
The variable 'result' is declared but not initialized, so it is undefined initially.Step 2: Understand XOR with undefined
XOR with undefined causes NaN or runtime error; it must start at 0.Final Answer:
Variable 'result' is not initialized -> Option AQuick Check:
Initialize result = 0 before XOR loop [OK]
- Forgetting to initialize result
- Assuming undefined XOR number works
- Incorrect loop boundaries
const arr = [5, 5, 5, 7, 7, 8, 8];
Solution
Step 1: Understand XOR behavior with odd counts
XOR cancels even appearances (like twice) to 0, but leaves the result of odd appearances (like thrice: x ^ x ^ x = 0, not x).Step 2: Analyze the given array
5 appears thrice (odd), 7 and 8 twice (even); XOR of all yields 0, so XOR alone cannot find the unique element repeated thrice.Final Answer:
No, XOR fails because it only cancels pairs, not triples -> Option AQuick Check:
XOR fails to find element repeated thrice [OK]
- Thinking XOR works for triples
- Believing sorting is required for XOR
- Confusing XOR with requiring exactly one unique
Ready to prove it?
Pass the test and this pattern moves unknown
