Find the Only Non Repeating Element Using XOR in DSA Javascript - Time & Space Complexity
We want to know how the time needed to find the unique element changes as the list grows.
How does the number of steps grow when the input list gets bigger?
Analyze the time complexity of the following code snippet.
function findUnique(arr) {
let result = 0;
for (let num of arr) {
result ^= num;
}
return result;
}
This code finds the only number that does not repeat in the array by using XOR on all elements.
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 gets bigger, the number of steps 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 steps increase in a straight line as input size grows.
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 the time is constant no matter the list size."
[OK] Correct: Even though XOR is fast, we still must apply it to every element, so time grows with list size.
Understanding this helps you quickly explain how simple operations on all items lead to efficient solutions.
"What if the array was sorted? Would the time complexity change if we used a different method?"
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 ^ 5 = 0 as each bit is the same and cancels.Final Answer:
It returns 0 -> Option AQuick Check:
X ^ X = 0 [OK]
- Thinking XOR returns the number itself
- Confusing XOR with OR operation
- Assuming XOR sums numbers
a and b in JavaScript?Solution
Step 1: Recall JavaScript bitwise XOR operator
In JavaScript, the XOR operator is represented by the caret symbol ^.Step 2: Check each option
a ^ b uses ^ correctly. a xor b uses 'xor' which is not valid syntax. Options A and D are logical AND and bitwise OR respectively.Final Answer:
a ^ b -> Option BQuick Check:
Use ^ for XOR in JavaScript [OK]
- Using 'xor' keyword
- Confusing XOR with && or ||
- Using | instead of ^
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
Start with result = 0, then XOR with each element: 0 ^ 2 = 2, 2 ^ 3 = 1, 1 ^ 2 = 3, 3 ^ 4 = 7, 7 ^ 3 = 4.Step 2: Final XOR result is the non-repeating element
The only element that does not repeat is 4, which is the final XOR result.Final Answer:
4 -> Option CQuick Check:
Final XOR = 4 [OK]
- Not starting XOR from 0
- XORing incorrectly in loop
- 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 initialization of 'result'
The variable 'result' is declared but not initialized, so it is undefined initially.Step 2: Effect of uninitialized 'result' on XOR
XOR with undefined results in NaN, so the final output will be incorrect. Initializing 'result' to 0 fixes this.Final Answer:
Variable 'result' is not initialized to 0 -> Option AQuick Check:
Initialize result = 0 before XOR loop [OK]
- Not initializing XOR variable
- Using wrong loop condition
- Misusing XOR operator
Solution
Step 1: Understand XOR behavior with repeated elements
XOR cancels out pairs of identical numbers because x ^ x = 0.Step 2: Analyze effect of triply repeated elements
Elements repeated thrice do not cancel out with XOR because XOR of three identical numbers is the number itself, not zero.Step 3: Conclusion on XOR usage
Therefore, XOR alone cannot find the unique element if others repeat thrice; other methods are needed.Final Answer:
No, because XOR only cancels pairs of identical numbers -> Option DQuick Check:
XOR cancels pairs, not triples [OK]
- Assuming XOR works for any repetition count
- Thinking sorting helps XOR
- Confusing XOR with sum or count
Ready to prove it?
Pass the test and this pattern moves unknown
