Find the Only Non Repeating Element Using XOR in DSA C++ - Time & Space Complexity
We want to know how fast we can find the single number that appears only once in a list where every other number appears twice.
How does the time needed grow as the list gets bigger?
Analyze the time complexity of the following code snippet.
int findUnique(int arr[], int n) {
int result = 0;
for (int i = 0; i < n; i++) {
result ^= arr[i];
}
return result;
}
This code uses XOR to find the only element that does not repeat in the array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: One loop that goes through each element once.
- How many times: Exactly n times, where n is the number of elements.
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 operations 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 need to check every element once, so time grows with the list size.
Understanding this approach shows you can use clever tricks to solve problems efficiently, a skill valued in real coding challenges.
"What if the array was sorted? Would that change the time complexity of finding the unique element?"
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 AQuick Check:
X XOR X = 0 [OK]
- Thinking XOR returns the number itself
- Confusing XOR with OR or AND
- Assuming XOR returns 1 for identical numbers
a and b in C++?Solution
Step 1: Identify XOR operator in C++
The XOR operator in C++ is the caret symbol (^).Step 2: Check syntax correctness
Usingint result = a ^ b;correctly computes XOR of a and b.Final Answer:
int result = a ^ b; -> Option BQuick Check:
Use ^ for XOR in C++ [OK]
- Using && instead of ^ for XOR
- Using | or & which are OR and AND
- Missing semicolon at end
int arr[] = {2, 3, 5, 4, 5, 3, 4};
int result = 0;
for(int i = 0; i < 7; i++) {
result ^= arr[i];
}
std::cout << result << std::endl;Solution
Step 1: XOR all elements in the array
Perform XOR stepwise: 0^2=2, 2^3=1, 1^5=4, 4^4=0, 0^5=5, 5^3=6, 6^4=2.Step 2: Identify the non-repeating element
All repeating elements cancel out, leaving 2 as the only non-repeating element.Final Answer:
2 -> Option AQuick Check:
Only non-repeating element = 2 [OK]
- Not XORing all elements correctly
- Confusing output with a repeating element
- Assuming sum instead of XOR
int arr[] = {1, 2, 1, 3, 2};
int result;
for(int i = 0; i < 5; i++) {
result ^= arr[i];
}
std::cout << result << std::endl;Solution
Step 1: Check variable initialization
The variable 'result' is declared but not initialized, so XOR with garbage value leads to wrong result.Step 2: Correct initialization
Initialize 'result' to 0 before the loop to correctly accumulate XOR.Final Answer:
Variable 'result' is not initialized before XOR operation -> Option CQuick Check:
Initialize XOR accumulator to 0 [OK]
- Not initializing XOR variable
- Using wrong operator instead of ^
- Incorrect loop bounds
Solution
Step 1: Use XOR properties for unique element
XORing all elements cancels out duplicates because X XOR X = 0, leaving only the unique element.Step 2: Implement efficient XOR approach
Initialize a variable to 0 and XOR it with each element in one pass for O(n) time and O(1) space.Final Answer:
Initialize a variable to 0 and XOR it with every element in the array once -> Option DQuick Check:
Single pass XOR finds unique element efficiently [OK]
- Using sorting which is slower
- Using extra space with hash maps
- Incorrect arithmetic approach
Ready to prove it?
Pass the test and this pattern moves unknown
