Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the result variable to zero.
DSA Rust
fn find_unique(nums: &[i32]) -> i32 {
let mut result = [1];
for &num in nums {
result ^= num;
}
result
}Attempts:
3 left
💡 Hint
Common Mistakes
Initializing result with nums[0] causes skipping the first element in loop.
Using 1 or -1 changes the XOR result incorrectly.
✗ Incorrect
We start with zero because XOR with zero leaves the number unchanged, allowing us to accumulate XOR of all elements.
2fill in blank
mediumComplete the code to XOR each number with the result.
DSA Rust
fn find_unique(nums: &[i32]) -> i32 {
let mut result = 0;
for &num in nums {
result [1] num;
}
result
}Attempts:
3 left
💡 Hint
Common Mistakes
Using += or other arithmetic operators instead of XOR.
Forgetting to use the assignment operator.
✗ Incorrect
XOR assignment (^=) updates result by XORing it with num, which helps find the unique element.
3fill in blank
hardFix the error in the function signature to accept a slice of integers.
DSA Rust
fn find_unique([1]) -> i32 { let mut result = 0; for &num in nums { result ^= num; } result }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Vec requires ownership and may cause unnecessary copying.
Using i32 is incorrect because we need a collection.
✗ Incorrect
Using a slice &[i32] allows the function to accept any slice of integers without ownership issues.
4fill in blank
hardFill both blanks to create a function that returns the unique element using XOR.
DSA Rust
fn find_unique(nums: [1]) -> [2] { let mut result = 0; for &num in nums { result ^= num; } result }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning Vec instead of i32.
Using usize as return type which is incorrect here.
✗ Incorrect
The function takes a slice of i32 (&[i32]) and returns a single i32 value representing the unique element.
5fill in blank
hardFill all three blanks to complete the main function that tests find_unique.
DSA Rust
fn main() {
let nums = [1];
let unique = find_unique([2]);
println!("Unique element is: {}", [3]);
}Attempts:
3 left
💡 Hint
Common Mistakes
Passing nums without reference causes type mismatch.
Printing nums instead of unique.
✗ Incorrect
We create a vector nums, pass a reference &nums to find_unique, and print the unique variable.
