Bird
Raised Fist0
DSA Rustprogramming~20 mins

Find the Only Non Repeating Element Using XOR in DSA Rust - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
XOR Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Rust code using XOR to find the unique element?
Consider the Rust code below that finds the only non-repeating element in an array using XOR. What will it print?
DSA Rust
fn find_unique(arr: &[i32]) -> i32 {
    let mut result = 0;
    for &num in arr {
        result ^= num;
    }
    result
}

fn main() {
    let nums = [2, 3, 5, 4, 5, 3, 4];
    println!("{}", find_unique(&nums));
}
A2
B5
C3
D4
Attempts:
2 left
💡 Hint
XOR of a number with itself is zero, and XOR with zero is the number itself.
❓ Predict Output
intermediate
2:00remaining
What is the output of this Rust code with negative numbers?
What will this Rust program print when finding the unique element using XOR with negative numbers?
DSA Rust
fn find_unique(arr: &[i32]) -> i32 {
    let mut result = 0;
    for &num in arr {
        result ^= num;
    }
    result
}

fn main() {
    let nums = [-1, -2, -2, -3, -1];
    println!("{}", find_unique(&nums));
}
A-1
B-2
C0
D-3
Attempts:
2 left
💡 Hint
XOR works the same with negative numbers as with positive numbers.
🔧 Debug
advanced
2:00remaining
What error does this Rust code produce?
This Rust code tries to find the unique element using XOR but has a mistake. What error will it cause?
DSA Rust
fn find_unique(arr: &[i32]) -> i32 {
    let mut result = 0;
    for num in arr {
        result ^= num;
    }
    result
}

fn main() {
    let nums = [1, 2, 1];
    println!("{}", find_unique(&nums));
}
AType mismatch error because num is a reference
BNo error, prints 2
CSyntax error due to missing semicolon
DRuntime panic due to invalid XOR operation
Attempts:
2 left
💡 Hint
Check the type of 'num' in the for loop and how XOR is applied.
🧠 Conceptual
advanced
2:00remaining
Why does XOR help find the unique element in an array where others repeat twice?
Which statement best explains why XOR can find the only non-repeating element in an array where every other element appears exactly twice?
AXOR multiplies all numbers, so the unique number is the product
BXOR of two identical numbers is zero, so pairs cancel out, leaving the unique number
CXOR adds all numbers together, so the unique number is the sum
DXOR sorts the array, so the unique number is at the start
Attempts:
2 left
💡 Hint
Think about what happens when you XOR a number with itself.
❓ Predict Output
expert
3:00remaining
What is the output of this Rust code with multiple unique elements?
What will this Rust program print when the array has two unique elements and others repeated twice?
DSA Rust
fn find_unique(arr: &[i32]) -> i32 {
    let mut result = 0;
    for &num in arr {
        result ^= num;
    }
    result
}

fn main() {
    let nums = [1, 2, 3, 2, 1, 4];
    println!("{}", find_unique(&nums));
}
A0
B4
C7
D3
Attempts:
2 left
💡 Hint
XOR of two unique numbers is their XOR combination, not one of them.

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

  1. Step 1: Understand XOR property for identical numbers

    XOR of a number with itself is always 0 because bits cancel out.
  2. Step 2: Apply this property to the question

    Since the two numbers are identical, XOR returns 0.
  3. Final Answer:

    It returns 0 -> Option A
  4. 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

  1. Step 1: Identify mutable variable declaration

    We need a mutable variable to update XOR results, so use 'mut'.
  2. Step 2: Initialize variable to zero

    Zero is the identity for XOR, so initialize with 0.
  3. Final Answer:

    let mut result = 0; -> Option A
  4. 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);
}
medium
A. 4
B. 5
C. 0
D. 2

Solution

  1. 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.
  2. Step 2: Identify the non-repeating element

    The final XOR result is 2, which is the only number not repeated.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Final XOR = 2 [OK]
Hint: XOR all numbers; result is unique element [OK]
Common Mistakes:
  • Miscomputing XOR steps
  • Assuming repeated numbers affect result
  • Confusing output with repeated elements
4. Identify the error in this Rust code that tries to find the non-repeating element using XOR:
fn main() {
    let arr = [1, 2, 1, 3, 2];
    let mut result = 0;
    for num in arr {
        result = result ^ num;
    }
    println!("{}", result);
}
medium
A. Missing '&' in for loop to borrow elements
B. No error, code works correctly
C. Incorrect initialization of result variable
D. Using '=' instead of '^=' operator

Solution

  1. Step 1: Check for borrowing in for loop

    Iterating over array by value is allowed; no '&' needed.
  2. Step 2: Verify XOR operation and initialization

    Result initialized to 0 and XOR applied correctly with '=' operator.
  3. Final Answer:

    No error, code works correctly -> Option B
  4. Quick Check:

    Code runs and finds unique element [OK]
Hint: XOR with '=' works; no borrow needed for array iteration [OK]
Common Mistakes:
  • Thinking '&' is mandatory in for loop
  • Confusing '=' with '^=' operator
  • Assuming initialization must be different
5. Given an array where every element repeats twice except one element which repeats thrice, can XOR find the unique element? Why or why not?
hard
A. Yes, XOR works for any number of duplicates
B. No, XOR works only if unique element appears once
C. No, XOR only works if all duplicates appear exactly twice
D. Yes, but only if array length is odd

Solution

  1. Step 1: Understand XOR behavior with duplicates

    XOR cancels pairs of identical numbers (duplicates appearing twice).
  2. Step 2: Analyze effect of triplicate duplicates

    Triplicates do not cancel out with XOR, so result is incorrect.
  3. Final Answer:

    No, XOR only works if all duplicates appear exactly twice -> Option C
  4. Quick Check:

    XOR cancels pairs, not triplets [OK]
Hint: XOR cancels pairs only, not triplets [OK]
Common Mistakes:
  • Assuming XOR works for any duplicate count
  • Confusing unique element count with duplicates
  • Ignoring XOR cancellation properties
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard