Bird
Raised Fist0
DSA C++programming~20 mins

Remove Duplicates from Sorted Array Two Pointer in DSA C++ - 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 array after removing duplicates using the two-pointer method on the sorted array?
DSA C++
int removeDuplicates(vector<int>& nums) {
    if (nums.empty()) return 0;
    int i = 0;
    for (int j = 1; j < nums.size(); j++) {
        if (nums[j] != nums[i]) {
            i++;
            nums[i] = nums[j];
        }
    }
    return i + 1;
}

vector<int> nums = {1,1,2,2,3,4,4,5};
int length = removeDuplicates(nums);
for (int k = 0; k < length; k++) {
    cout << nums[k] << " ";
}
A1 2 3 4 4 5
B1 1 2 2 3 4 4 5
C1 2 3 4 5
D1 2 2 3 4 5
Attempts:
2 left
💡 Hint
Focus on how the two pointers move and overwrite duplicates.
🧠 Conceptual
intermediate
1:30remaining
Understanding Two Pointer Roles
In the two-pointer approach to remove duplicates from a sorted array, what is the main role of the slow pointer?
ATo mark the position where the next unique element should be placed
BTo scan through the array and find unique elements
CTo count the total number of duplicates
DTo swap elements to sort the array
Attempts:
2 left
💡 Hint
Think about how the array is modified in-place.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Duplicate Removal Code
What error will this code cause when removing duplicates from a sorted array? int removeDuplicates(vector& nums) { int i = 0; for (int j = 1; j < nums.size(); j++) { if (nums[j] != nums[i]) { i++; nums[j] = nums[i]; } } return i + 1; }
AIt will produce incorrect output because elements are overwritten incorrectly
BIt will compile but return zero always
CIt will cause a runtime error due to out-of-bounds access
DIt will work correctly and remove duplicates
Attempts:
2 left
💡 Hint
Check the assignment inside the if condition carefully.
❓ Predict Output
advanced
1:30remaining
Output Length After Removing Duplicates
What is the returned length after removing duplicates from the sorted array using the two-pointer method?
DSA C++
vector<int> nums = {0,0,1,1,1,2,2,3,3,4};
int removeDuplicates(vector<int>& nums) {
    if (nums.empty()) return 0;
    int i = 0;
    for (int j = 1; j < nums.size(); j++) {
        if (nums[j] != nums[i]) {
            i++;
            nums[i] = nums[j];
        }
    }
    return i + 1;
}
int length = removeDuplicates(nums);
cout << length;
A6
B5
C4
D10
Attempts:
2 left
💡 Hint
Count unique elements in the array.
🚀 Application
expert
2:30remaining
Applying Two Pointer Method on Large Sorted Array
Given a sorted array of 1 million elements with many duplicates, which is the best reason to use the two-pointer method to remove duplicates in-place?
AIt sorts the array first to group duplicates together
BIt uses recursion to remove duplicates efficiently
CIt uses extra memory proportional to the array size for a new array
DIt removes duplicates without extra space and in a single pass
Attempts:
2 left
💡 Hint
Think about memory and time efficiency for large data.

Practice

(1/5)
1. What is the main purpose of the two-pointer technique in removing duplicates from a sorted array?
easy
A. To track unique elements and overwrite duplicates in-place
B. To sort the array again after removing duplicates
C. To create a new array with duplicates removed
D. To count the total number of duplicates only

Solution

  1. Step 1: Understand the two-pointer roles

    One pointer tracks the position to place unique elements, the other scans the array.
  2. Step 2: Overwrite duplicates in-place

    When a new unique element is found, it is placed at the tracked position, replacing duplicates.
  3. Final Answer:

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

    Two-pointer technique = overwrite duplicates [OK]
Hint: Two pointers: one scans, one writes unique values [OK]
Common Mistakes:
  • Thinking it creates a new array instead of modifying in-place
  • Assuming it sorts the array again
  • Confusing counting duplicates with removing them
2. Which of the following is the correct syntax to initialize the two pointers for removing duplicates from a sorted array nums in C++?
easy
A. int i = 0, j = 1;
B. int i = 1, j = 0;
C. int i = 0, j = 0;
D. int i = 1, j = 1;

Solution

  1. Step 1: Identify pointer roles

    Pointer i tracks the last unique element index, starting at 0; j scans from 1.
  2. Step 2: Initialize pointers accordingly

    Set i = 0 and j = 1 to start comparing from the second element.
  3. Final Answer:

    int i = 0, j = 1; -> Option A
  4. Quick Check:

    i=0, j=1 start positions [OK]
Hint: Start i at 0, j at 1 to compare neighbors [OK]
Common Mistakes:
  • Starting both pointers at 0
  • Swapping i and j initialization
  • Starting j before i
3. What will be the output length after removing duplicates using the two-pointer method on the sorted array nums = {1, 1, 2, 3, 3, 4}?
medium
A. 3
B. 4
C. 5
D. 6

Solution

  1. Step 1: Identify unique elements in order

    Unique elements are 1, 2, 3, 4.
  2. Step 2: Count unique elements

    There are 4 unique elements, so the length after removal is 4.
  3. Final Answer:

    4 -> Option B
  4. Quick Check:

    Unique count = 4 [OK]
Hint: Count unique numbers in sorted array [OK]
Common Mistakes:
  • Counting duplicates as unique
  • Confusing length with original array size
  • Off-by-one errors in counting
4. Identify the error in the following C++ code snippet for removing duplicates from a sorted array nums:
int removeDuplicates(vector<int>& nums) {
    int i = 0, j = 1;
    while (j < nums.size()) {
        if (nums[i] != nums[j]) {
            i++;
            nums[i] = nums[j];
        }
        j++;
    }
    return i;
}
medium
A. The loop should use for instead of while
B. Pointers i and j should start at 1 and 0 respectively
C. The condition should be nums[i] == nums[j] instead of !=
D. Return value should be i + 1, not i

Solution

  1. Step 1: Understand pointer i meaning

    Pointer i is index of last unique element, so length is i + 1.
  2. Step 2: Check return statement

    Returning i alone gives index, not count; must return i + 1 for length.
  3. Final Answer:

    Return value should be i + 1, not i -> Option D
  4. Quick Check:

    Length = last index + 1 [OK]
Hint: Return i + 1 as length, not i [OK]
Common Mistakes:
  • Returning i instead of i + 1
  • Swapping pointer initializations
  • Using wrong comparison operator
5. Given a sorted array nums = {0, 0, 1, 1, 1, 2, 2, 3, 3, 4}, what will be the state of the array after applying the two-pointer method to remove duplicates, and what is the new length?
hard
A. Array: {0, 1, 1, 2, 3, 4, _, _, _, _}, Length: 4
B. Array: {0, 0, 1, 1, 2, 2, 3, 3, 4, 4}, Length: 6
C. Array: {0, 1, 2, 3, 4, _, _, _, _, _}, Length: 5
D. Array: {0, 1, 2, 3, 4, 5, _, _, _, _}, Length: 6

Solution

  1. Step 1: Identify unique elements in order

    Unique elements are 0, 1, 2, 3, 4.
  2. Step 2: Overwrite duplicates in-place

    Using two pointers, these unique elements overwrite the start of the array.
  3. Step 3: Determine new length

    There are 5 unique elements, so length is 5.
  4. Final Answer:

    Array: {0, 1, 2, 3, 4, _, _, _, _, _}, Length: 5 -> Option C
  5. Quick Check:

    Unique elements = 5, array start updated [OK]
Hint: Unique elements overwrite start, length = count unique [OK]
Common Mistakes:
  • Not overwriting duplicates correctly
  • Miscounting unique elements
  • Assuming array length stays same
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