Discover how a simple XOR trick can save you hours of tedious searching!
Why Two Non Repeating Elements in Array Using XOR in DSA Rust?
Imagine you have a box of socks where every sock has a matching pair except two unique socks. You want to find those two unique socks quickly without checking every single sock against all others.
Manually comparing each sock with every other sock takes a lot of time and effort. It is easy to make mistakes and miss pairs, especially when the box is large. This slow and error-prone process wastes your time.
Using XOR, a simple math trick, you can find the two unique socks in just one pass through the box. XOR cancels out pairs automatically, leaving only the unique socks. This method is fast, clean, and reliable.
for i in 0..arr.len() { for j in i+1..arr.len() { if arr[i] == arr[j] { mark_as_paired(i, j); } } } find_unpaired();
let xor_all = arr.iter().fold(0, |acc, &x| acc ^ x); let set_bit = xor_all & (!(xor_all - 1)); let (mut num1, mut num2) = (0, 0); for &num in &arr { if num & set_bit == 0 { num1 ^= num; } else { num2 ^= num; } } println!("The two unique numbers are {} and {}", num1, num2);
This technique lets you find two unique items in a large collection instantly, without extra memory or complex checks.
Finding two unique error codes in a huge log file where every other error code appears twice, helping quickly identify rare issues.
Manual pair checking is slow and error-prone.
XOR cancels pairs and isolates unique elements efficiently.
Using XOR reduces time and space needed to find two unique numbers.
Practice
Solution
Step 1: Understand XOR operation on array elements
XORing all elements cancels out pairs of repeating elements because x ^ x = 0.Step 2: Result after XORing all elements
The result is XOR of the two unique elements since all duplicates cancel out.Final Answer:
XOR of all elements gives XOR of the two unique elements -> Option BQuick Check:
XOR cancels duplicates = XOR of unique elements [OK]
- Thinking XOR counts frequency
- Assuming XOR sorts elements
- Using XOR as sum instead of bitwise operation
arr?Solution
Step 1: Initialize mutable variable for XOR
Uselet mut xor = 0;to start XOR from zero.Step 2: Iterate over references to elements and XOR
Usefor &num in &arr { xor ^= num; }to XOR each element.Final Answer:
let mut xor = 0; for &num in &arr { xor ^= num; } -> Option AQuick Check:
Mutable xor with ^= in loop = correct syntax [OK]
- Using sum() instead of XOR
- Trying to multiply elements
- Not using mutable variable for XOR
let arr = vec![2, 3, 7, 9, 2, 3];
let mut xor = 0;
for &num in &arr {
xor ^= num;
}
println!("{}", xor);Solution
Step 1: XOR all elements in the array
Calculate 2 ^ 3 ^ 7 ^ 9 ^ 2 ^ 3 step-by-step: 2 ^ 3 = 1, 1 ^ 7 = 6, 6 ^ 9 = 15, 15 ^ 2 = 13, 13 ^ 3 = 14.Step 2: Final XOR value
The final XOR value is 14 after processing all elements.Final Answer:
14 -> Option CQuick Check:
XOR all elements = 14 [OK]
- Incorrect XOR calculation order
- Assuming sum instead of XOR
- Forgetting XOR cancels duplicates
let arr = vec![4, 1, 2, 1, 2, 5];
let mut xor = 0;
for num in arr {
xor ^= num;
}
let set_bit = xor & (-xor);
println!("{}", set_bit);Solution
Step 1: Understand variable types and operations
In Rust, integer literals default to i32 (signed), soxor & (-xor)is valid.Step 2: Verify code execution
Loop correctly computes XOR (1), set_bit calculates correctly (1). No syntax or runtime errors.Final Answer:
No error, code runs fine -> Option DQuick Check:
i32 supports unary minus operator [OK]
- Misassuming unsigned integers by default
- Thinking 'for num in arr' syntax is wrong
- Expecting mutable keyword issues
arr = [6, 1, 3, 6, 1, 5], which Rust code snippet correctly finds the two non-repeating elements using XOR?Solution
Step 1: XOR all elements to get xor of two unique numbers
Loop over array and XOR all elements to get xor = 6 ^ 1 ^ 3 ^ 6 ^ 1 ^ 5 = 3 ^ 5 = 6.Step 2: Find rightmost set bit and separate elements
Calculate set_bit = xor & xor.wrapping_neg() to isolate differing bit. Then separate elements into two groups based on this bit and XOR each group to find unique elements.Step 3: Print the two unique elements
After separation and XOR, x and y hold the two unique numbers 3 and 5.Final Answer:
Code snippet in Option A correctly finds two unique elements -> Option AQuick Check:
Use XOR, set bit, separate groups = correct approach [OK]
- Using sum or multiplication instead of XOR
- Not isolating rightmost set bit correctly
- Not separating elements into two groups
Ready to prove it?
Pass the test and this pattern moves unknown
