Discover how a simple trick can instantly find the one odd item out in a crowd!
Why Find the Only Non Repeating Element Using XOR in DSA Rust?
Imagine you have a box of socks where every sock has a matching pair except one. You want to find that single sock without checking each pair one by one.
Checking each sock manually to find the one without a pair takes a lot of time and can easily lead to mistakes, especially if the box is big. You might lose track or mix socks up.
Using XOR, you can quickly find the single sock without a pair by comparing all socks together. XOR cancels out pairs automatically, leaving only the unmatched sock.
let mut count = std::collections::HashMap::new(); for &sock in &socks { *count.entry(sock).or_insert(0) += 1; } for (&sock, ×) in &count { if times == 1 { return sock; } }
let mut unique_sock = 0; for &sock in &socks { unique_sock ^= sock; } return unique_sock;
This method lets you find the single unmatched item in a list instantly, no matter how large the list is.
Finding the one defective product in a batch where all others are identical and paired correctly.
Manual checking is slow and error-prone.
XOR cancels out pairs automatically.
Efficiently finds the single 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
