Bird
Raised Fist0
DSA Rustprogramming~20 mins

Remove Duplicates from Sorted Array Two Pointer in DSA Rust - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Two Pointer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Two Pointer Duplicate Removal
What is the output of the following Rust code that removes duplicates from a sorted array using two pointers?
DSA Rust
fn remove_duplicates(nums: &mut Vec<i32>) -> usize {
    if nums.is_empty() {
        return 0;
    }
    let mut i = 0;
    for j in 1..nums.len() {
        if nums[j] != nums[i] {
            i += 1;
            nums[i] = nums[j];
        }
    }
    i + 1
}

fn main() {
    let mut nums = vec![1,1,2,2,3,4,4,5];
    let len = remove_duplicates(&mut nums);
    nums.truncate(len);
    println!("{:?}", nums);
}
A[1, 2, 3, 4, 5]
B[1, 1, 2, 2, 3, 4, 4, 5]
C[1, 2, 2, 3, 4, 5]
D[1, 2, 3, 4]
Attempts:
2 left
💡 Hint
Focus on how the two pointers move and how duplicates are overwritten.
🧠 Conceptual
intermediate
1:30remaining
Understanding Two Pointer Technique
Why does the two pointer technique work efficiently for removing duplicates from a sorted array?
ABecause it sorts the array first to group duplicates together.
BBecause it uses extra memory to store unique elements separately.
CBecause the array is sorted, duplicates are adjacent, so one pointer can track unique elements while the other scans ahead.
DBecause it removes duplicates by swapping elements randomly.
Attempts:
2 left
💡 Hint
Think about how sorting affects duplicates placement.
❓ Predict Output
advanced
2:00remaining
Output with All Elements Identical
What will be the output of this Rust code when all elements in the array are identical?
DSA Rust
fn remove_duplicates(nums: &mut Vec<i32>) -> usize {
    if nums.is_empty() {
        return 0;
    }
    let mut i = 0;
    for j in 1..nums.len() {
        if nums[j] != nums[i] {
            i += 1;
            nums[i] = nums[j];
        }
    }
    i + 1
}

fn main() {
    let mut nums = vec![7,7,7,7,7];
    let len = remove_duplicates(&mut nums);
    nums.truncate(len);
    println!("{:?}", nums);
}
A[7]
B[7, 7, 7, 7, 7]
C[]
D[7, 7]
Attempts:
2 left
💡 Hint
Consider how many unique elements exist.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Duplicate Removal Code
What error will this Rust code produce when trying to remove duplicates from a sorted array?
DSA Rust
fn remove_duplicates(nums: &mut Vec<i32>) -> usize {
    let mut i = 0;
    for j in 1..nums.len() {
        if nums[j] != nums[i] {
            i += 1;
            nums[i] = nums[j];
        }
    }
    i + 1
}

fn main() {
    let mut nums = vec![];
    let len = remove_duplicates(&mut nums);
    nums.truncate(len);
    println!("{:?}", nums);
}
Ainfinite loop
Bprints [] with no error
Ccompilation error due to missing return
Dpanic at index 0 out of bounds
Attempts:
2 left
💡 Hint
Check what happens when the input vector is empty.
🚀 Application
expert
2:30remaining
Length of Array After Removing Duplicates
Given the Rust function below, what is the length of the array after removing duplicates from this input: [0,0,1,1,1,2,2,3,3,4]?
DSA Rust
fn remove_duplicates(nums: &mut Vec<i32>) -> usize {
    if nums.is_empty() {
        return 0;
    }
    let mut i = 0;
    for j in 1..nums.len() {
        if nums[j] != nums[i] {
            i += 1;
            nums[i] = nums[j];
        }
    }
    i + 1
}
A4
B5
C10
D6
Attempts:
2 left
💡 Hint
Count unique elements in the input array.

Practice

(1/5)
1. What is the main purpose of using two pointers in the Remove Duplicates from Sorted Array problem?
easy
A. To sort the array before removing duplicates
B. To efficiently track unique elements and overwrite duplicates in-place
C. To create a new array with unique elements
D. To count the total number of duplicates in the array

Solution

  1. Step 1: Understand the two-pointer approach

    One pointer tracks the position to place the next unique element, the other scans the array.
  2. Step 2: Purpose of pointers in-place

    This method avoids extra space by overwriting duplicates directly in the original array.
  3. Final Answer:

    To efficiently track unique elements and overwrite duplicates in-place -> Option B
  4. Quick Check:

    Two pointers help remove duplicates in-place [OK]
Hint: Two pointers move to overwrite duplicates without extra space [OK]
Common Mistakes:
  • Thinking two pointers create a new array
  • Assuming sorting is needed again
  • Counting duplicates instead of removing
2. Which of the following Rust code snippets correctly initializes two pointers for the Remove Duplicates problem?
easy
A. let mut i = 1; let mut j = 0;
B. let mut i = 0; let mut j = 1;
C. let i = 1; let j = 0;
D. let i = 0; let j = 0;

Solution

  1. Step 1: Identify pointer roles

    Pointer i tracks last unique index, starting at 0; j scans from 1.
  2. Step 2: Match initialization

    Correct initialization is let mut i = 1; let mut j = 0; to start comparing from second element.
  3. Final Answer:

    let mut i = 1; let mut j = 0; -> Option A
  4. Quick Check:

    i=1, j=0 starts scanning correctly [OK]
Hint: Start i at 1 and j at 0 to compare adjacent elements [OK]
Common Mistakes:
  • Swapping i and j initialization
  • Not using mutable variables
  • Starting both pointers at 0
3. What will be the content of the array after running the two-pointer duplicate removal on let mut nums = vec![1, 1, 2, 3, 3];?
medium
A. [1, 2, 3, 3, 3]
B. [1, 1, 2, 3, 3]
C. [1, 2, 3, _, _]
D. [1, 2, 3, 0, 0]

Solution

  1. Step 1: Apply two-pointer logic

    Start with i=0, j=1; compare nums[j] with nums[i]. When different, increment i and copy nums[j].
  2. Step 2: Trace array changes

    nums changes to [1, 2, 3, 3, 3], but only first 3 elements are unique; rest are leftover values.
  3. Final Answer:

    [1, 2, 3, _, _] -> Option C
  4. Quick Check:

    Unique elements at start, duplicates ignored [OK]
Hint: Unique elements fill front; leftover values remain unchanged [OK]
Common Mistakes:
  • Assuming duplicates are removed from array length
  • Expecting zeros instead of leftover values
  • Not understanding in-place overwrite
4. Identify the bug in this Rust code snippet for removing duplicates from a sorted array:
let mut i = 0;
for j in 1..nums.len() {
    if nums[i] == nums[j] {
        i += 1;
        nums[i] = nums[j];
    }
}
medium
A. Pointer i should not be incremented inside the if block
B. The loop range should start from 0, not 1
C. nums[i] should be compared with nums[i-1], not nums[j]
D. The condition should check for inequality, not equality

Solution

  1. Step 1: Analyze the if condition

    The code increments i and copies nums[j] only when nums[i] == nums[j], which is wrong.
  2. Step 2: Correct condition logic

    We want to copy nums[j] only when nums[j] != nums[i], meaning a new unique element found.
  3. Final Answer:

    The condition should check for inequality, not equality -> Option D
  4. Quick Check:

    Copy unique elements when different [OK]
Hint: Copy when elements differ, not when equal [OK]
Common Mistakes:
  • Incrementing i on duplicates
  • Wrong comparison operator
  • Loop starting index confusion
5. Given a sorted vector nums with possible duplicates, how would you modify the two-pointer approach to return the count of unique elements and also ensure the first part of the vector contains only these unique elements in Rust?
hard
A. Use two pointers i=0, j=1; increment i and copy nums[j] when nums[j] != nums[i]; return i+1 as count
B. Use a HashSet to collect unique elements, then overwrite nums with the set; return set size
C. Sort the vector again after removing duplicates; return length of sorted vector
D. Iterate with one pointer, remove duplicates by shifting elements left; return original length minus duplicates

Solution

  1. Step 1: Initialize pointers and iterate

    Set i=0 for last unique index, j=1 to scan; compare nums[j] with nums[i].
  2. Step 2: Copy unique elements and count

    If nums[j] != nums[i], increment i and copy nums[j] to nums[i]. After loop, unique count is i+1.
  3. Final Answer:

    Use two pointers i=0, j=1; increment i and copy nums[j] when nums[j] != nums[i]; return i+1 as count -> Option A
  4. Quick Check:

    Two-pointer overwrite returns unique count [OK]
Hint: Increment i only on new unique element, return i+1 [OK]
Common Mistakes:
  • Using extra space like HashSet unnecessarily
  • Re-sorting after removal
  • Incorrect count calculation
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