Find the Only Non Repeating Element Using XOR in DSA Rust - Time & Space Complexity
We want to know how fast we can find the single element that does not repeat in a list.
How does the time needed grow as the list gets bigger?
Analyze the time complexity of the following code snippet.
fn find_non_repeating(arr: &[i32]) -> i32 {
let mut result = 0;
for &num in arr {
result ^= num;
}
result
}
This code finds the only number that appears once by using XOR on all elements.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: One loop that goes through each element once.
- How many times: Exactly once per element in the list.
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.
[X] Wrong: "Using XOR means the time is constant no matter the list size."
[OK] Correct: Even though XOR is fast, we must do it for every element, so time grows with list size.
Understanding this method shows you can use clever tricks to solve problems efficiently, a skill valued in real coding tasks.
"What if the list was sorted? Would that change the time complexity of finding the non-repeating element?"
Practice
Solution
Step 1: Understand XOR property for identical numbers
XOR of a number with itself is always 0 because bits cancel out.Step 2: Apply this property to the question
Since the two numbers are identical, XOR returns 0.Final Answer:
It returns 0 -> Option AQuick Check:
X XOR X = 0 [OK]
- Thinking XOR returns the number itself
- Confusing XOR with addition
- Assuming XOR returns 1 for identical numbers
Solution
Step 1: Identify mutable variable declaration
We need a mutable variable to update XOR results, so use 'mut'.Step 2: Initialize variable to zero
Zero is the identity for XOR, so initialize with 0.Final Answer:
let mut result = 0; -> Option AQuick Check:
Mutable zero initialization for XOR [OK]
- Not using 'mut' for variable to update
- Initializing with 1 instead of 0
- Using Option type instead of integer
fn main() {
let arr = [2, 3, 5, 4, 5, 3, 4];
let mut result = 0;
for &num in &arr {
result ^= num;
}
println!("{}", result);
}Solution
Step 1: XOR all elements in the array
Calculate 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
The final XOR result is 2, which is the only number not repeated.Final Answer:
2 -> Option DQuick Check:
Final XOR = 2 [OK]
- Miscomputing XOR steps
- Assuming repeated numbers affect result
- Confusing output with repeated elements
fn main() {
let arr = [1, 2, 1, 3, 2];
let mut result = 0;
for num in arr {
result = result ^ num;
}
println!("{}", result);
}Solution
Step 1: Check for borrowing in for loop
Iterating over array by value is allowed; no '&' needed.Step 2: Verify XOR operation and initialization
Result initialized to 0 and XOR applied correctly with '=' operator.Final Answer:
No error, code works correctly -> Option BQuick Check:
Code runs and finds unique element [OK]
- Thinking '&' is mandatory in for loop
- Confusing '=' with '^=' operator
- Assuming initialization must be different
Solution
Step 1: Understand XOR behavior with duplicates
XOR cancels pairs of identical numbers (duplicates appearing twice).Step 2: Analyze effect of triplicate duplicates
Triplicates do not cancel out with XOR, so result is incorrect.Final Answer:
No, XOR only works if all duplicates appear exactly twice -> Option CQuick Check:
XOR cancels pairs, not triplets [OK]
- Assuming XOR works for any duplicate count
- Confusing unique element count with duplicates
- Ignoring XOR cancellation properties
Ready to prove it?
Pass the test and this pattern moves unknown
