Two Non Repeating Elements in Array Using XOR in DSA Rust - Time & Space Complexity
We analyze how the time needed to find two unique numbers in an array grows as the array gets bigger.
We want to know how many steps the program takes when the input size changes.
Analyze the time complexity of the following code snippet.
fn find_two_unique(nums: &[i32]) -> (i32, i32) {
let mut xor_all = 0;
for &num in nums {
xor_all ^= num;
}
let rightmost_set_bit = xor_all & xor_all.wrapping_neg();
let (mut x, mut y) = (0, 0);
for &num in nums {
if num & rightmost_set_bit != 0 {
x ^= num;
} else {
y ^= num;
}
}
(x, y)
}
This code finds two numbers that appear only once in an array where all other numbers appear twice, using XOR operations.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two separate loops over the array.
- How many times: Each loop runs once over all elements, so twice in total.
As the array size grows, the number of steps grows roughly twice as fast because we loop twice over all elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 steps (2 loops x 10 elements) |
| 100 | About 200 steps |
| 1000 | About 2000 steps |
Pattern observation: The steps grow linearly with input size, doubling because of two passes.
Time Complexity: O(n)
This means the time to find the two unique numbers grows directly in proportion to the size of the input array.
[X] Wrong: "Since there are two loops, the time complexity is O(n²)."
[OK] Correct: The loops run one after another, not nested, so their times add up, not multiply. This keeps complexity linear, not quadratic.
Understanding this linear time solution shows you can use bitwise operations smartly to solve problems efficiently, a skill valued in many coding challenges.
"What if the array contained three unique numbers instead of two? How would the time complexity change?"
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
