📖 Scenario: Imagine you have a list of numbers where every number appears twice except one number that appears only once. You want to find that unique number quickly.
🎯 Goal: Build a Rust program that finds the only non-repeating element in a list of integers using the XOR operation.
📋 What You'll Learn
Create a vector called numbers with the exact values: [2, 3, 5, 4, 5, 3, 4]
Create a variable called unique and set it to 0
Use a for loop with variable num to iterate over numbers.iter() and update unique by XORing it with *num
Print the value of unique using println!("{}", unique)
💡 Why This Matters
🌍 Real World
Finding a unique element quickly is useful in error detection, data cleaning, and signal processing.
💼 Career
Understanding XOR and loops is fundamental for software engineers working with algorithms and low-level data manipulation.
Progress0 / 4 steps
1
Create the list of numbers
Create a vector called numbers with the exact values: [2, 3, 5, 4, 5, 3, 4]
DSA Rust
Hint
Use let numbers = vec![2, 3, 5, 4, 5, 3, 4]; to create the vector.
2
Initialize the XOR accumulator
Create a variable called unique and set it to 0 inside the main function after the numbers vector.
DSA Rust
Hint
Use let mut unique = 0; to create a mutable variable for XOR accumulation.
3
Apply XOR to find the unique number
Use a for loop with variable num to iterate over numbers.iter() and update unique by XORing it with *num inside the main function.
DSA Rust
Hint
Use for num in numbers.iter() and update unique with unique ^= *num;.
4
Print the unique number
Print the value of unique using println!("{}", unique) inside the main function.
DSA Rust
Hint
Use println!("{}", unique); to display the unique number.
Practice
(1/5)
1. What does the XOR operation do when applied between two identical numbers?
easy
A. It returns 0
B. It returns the number itself
C. It returns 1
D. It returns the sum of the numbers
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 A
Quick Check:
X XOR X = 0 [OK]
Hint: XOR of same numbers always zero [OK]
Common Mistakes:
Thinking XOR returns the number itself
Confusing XOR with addition
Assuming XOR returns 1 for identical numbers
2. Which Rust syntax correctly initializes a variable to zero for XOR operation?
easy
A. let mut result = 0;
B. let result = 0;
C. let mut result = 1;
D. let mut result = None;
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 A
Quick Check:
Mutable zero initialization for XOR [OK]
Hint: Use 'let mut var = 0;' for XOR accumulator [OK]
Common Mistakes:
Not using 'mut' for variable to update
Initializing with 1 instead of 0
Using Option type instead of integer
3. What is the output of this Rust code?
fn main() {
let arr = [2, 3, 5, 4, 5, 3, 4];
let mut result = 0;
for &num in &arr {
result ^= num;
}
println!("{}", result);
}