Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Remove Duplicates from Sorted Array Two Pointer in DSA Python - 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 removing duplicates using two pointers
What is the output array after removing duplicates from the sorted array using the two-pointer technique?
DSA Python
def remove_duplicates(nums):
    if not nums:
        return 0
    i = 0
    for j in range(1, len(nums)):
        if nums[j] != nums[i]:
            i += 1
            nums[i] = nums[j]
    return nums[:i+1]

result = remove_duplicates([1,1,2,2,3,4,4,5])
print(result)
A[1, 2, 3, 4]
B[1, 1, 2, 3, 4, 5]
C[1, 2, 2, 3, 4, 5]
D[1, 2, 3, 4, 5]
Attempts:
2 left
💡 Hint
Focus on how the two pointers move and overwrite duplicates.
❓ Predict Output
intermediate
2:00remaining
Length of array after removing duplicates
What is the length of the array after removing duplicates using the two-pointer method on this input?
DSA Python
def remove_duplicates(nums):
    if not nums:
        return 0
    i = 0
    for j in range(1, len(nums)):
        if nums[j] != nums[i]:
            i += 1
            nums[i] = nums[j]
    return i + 1

length = remove_duplicates([0,0,1,1,1,2,2,3,3,4])
print(length)
A4
B5
C7
D6
Attempts:
2 left
💡 Hint
Count how many unique elements remain after processing.
🔧 Debug
advanced
2:00remaining
Identify the error in two-pointer duplicate removal
What error will this code produce when removing duplicates from a sorted array?
DSA Python
def remove_duplicates(nums):
    i = 0
    for j in range(0, len(nums)):
        if nums[j] == nums[i]:
            i += 1
            nums[i] = nums[j]
    return nums[:i+1]

print(remove_duplicates([1,1,2,3,3]))
AIndexError
BTypeError
C[1, 1, 2, 3, 3]
D[1, 2, 3]
Attempts:
2 left
💡 Hint
Check the condition inside the loop and how i is incremented.
❓ Predict Output
advanced
2:00remaining
Final array state after removing duplicates with extra elements
What is the final state of the array after removing duplicates from [2,2,2,3,3,4,5,5,6] using the two-pointer method?
DSA Python
def remove_duplicates(nums):
    if not nums:
        return []
    i = 0
    for j in range(1, len(nums)):
        if nums[j] != nums[i]:
            i += 1
            nums[i] = nums[j]
    return nums[:i+1]

result = remove_duplicates([2,2,2,3,3,4,5,5,6])
print(result)
A[2, 3, 3, 4, 5, 6]
B[2, 2, 3, 4, 5, 6]
C[2, 3, 4, 5, 6]
D[2, 3, 4, 5]
Attempts:
2 left
💡 Hint
Only unique elements remain in the returned slice.
🧠 Conceptual
expert
2:00remaining
Why does the two-pointer method work for removing duplicates in sorted arrays?
Which explanation best describes why the two-pointer technique correctly removes duplicates from a sorted array?
ABecause the array is sorted, duplicates are adjacent, allowing one pointer to track unique elements and the other to scan ahead, overwriting duplicates efficiently.
BBecause the array is sorted, the pointers can jump over duplicates without checking each element, reducing time complexity to O(log n).
CBecause the array is sorted, the two pointers swap elements to sort the array again while removing duplicates.
DBecause the array is sorted, the two pointers create a new array with only unique elements by copying elements one by one.
Attempts:
2 left
💡 Hint
Think about adjacency of duplicates in sorted arrays.

Practice

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

Solution

  1. Step 1: Understand the two pointer roles

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

    When a new unique element is found, it is placed at the first pointer's position, avoiding extra space.
  3. Final Answer:

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

    Two pointers help remove duplicates without extra space [OK]
Hint: Two pointers help overwrite duplicates without extra space [OK]
Common Mistakes:
  • Thinking two pointers sort the array
  • Assuming a new array is created
  • Confusing counting duplicates with removing them
2. Which of the following is the correct syntax to initialize two pointers i and j for the two-pointer approach in Python?
easy
A. i = 1; j = 0
B. i = j = 0
C. i, j = 0, 1
D. i = 0; j = 0

Solution

  1. Step 1: Identify pointer roles

    Pointer i starts at 0 (position for unique elements), j starts at 1 (scanning pointer).
  2. Step 2: Match initialization

    Correct initialization is i, j = 0, 1 to start comparing adjacent elements.
  3. Final Answer:

    i, j = 0, 1 -> Option C
  4. Quick Check:

    Start pointers at 0 and 1 for two-pointer scan [OK]
Hint: Start pointers at 0 and 1 to compare adjacent elements [OK]
Common Mistakes:
  • Initializing both pointers at 0
  • Swapping pointer start positions
  • Using incorrect syntax for multiple assignments
3. What will be the output of the following code snippet?
nums = [1, 1, 2, 2, 3]
i = 0
for j in range(1, len(nums)):
    if nums[j] != nums[i]:
        i += 1
        nums[i] = nums[j]
print(nums[:i+1])
medium
A. [1, 1, 2, 3]
B. [1, 2, 3]
C. [1, 2, 2, 3]
D. [1, 1, 2, 2, 3]

Solution

  1. Step 1: Trace the loop and pointer updates

    Initially, i=0, j scans from 1 to 4. When nums[j] != nums[i], i increments and nums[i] is updated.
  2. Step 2: Track array changes

    After processing, nums becomes [1, 2, 3, 2, 3], but only first i+1=3 elements are unique.
  3. Final Answer:

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

    Unique elements extracted as [1, 2, 3] [OK]
Hint: Print slice up to i+1 for unique elements [OK]
Common Mistakes:
  • Printing full array instead of slice
  • Not updating i correctly
  • Confusing pointer roles
4. Identify the error in the following code snippet for removing duplicates from a sorted array:
nums = [1, 1, 2, 3, 3]
i = 0
for j in range(1, len(nums)):
    if nums[i] == nums[j]:
        i += 1
        nums[i] = nums[j]
print(nums[:i+1])
medium
A. The condition should check if nums[j] != nums[i]
B. The pointer i should start at 1 instead of 0
C. The loop should start from j = 0
D. The print statement should print the entire array

Solution

  1. Step 1: Analyze the if condition

    The code increments i and updates nums[i] only when nums[i] == nums[j], which is incorrect for removing duplicates.
  2. Step 2: Correct the condition

    It should increment i and update nums[i] when nums[j] != nums[i] to keep unique elements.
  3. Final Answer:

    The condition should check if nums[j] != nums[i] -> Option A
  4. Quick Check:

    Use != to detect new unique elements [OK]
Hint: Use != to find unique elements, not == [OK]
Common Mistakes:
  • Using == instead of !=
  • Starting pointers incorrectly
  • Printing wrong slice of array
5. Given a sorted array nums = [0,0,1,1,1,2,2,3,3,4], what will be the array state after applying the two-pointer method to remove duplicates in-place?
hard
A. [0, 1, 2, 3, 4]
B. [0, 0, 1, 2, 3, 4]
C. [0, 1, 1, 2, 3, 4]
D. [0, 1, 2, 2, 3, 4]

Solution

  1. Step 1: Apply two-pointer logic on nums

    Start with i=0, j scans from 1 to end. Each time nums[j] != nums[i], increment i and set nums[i] = nums[j].
  2. Step 2: Track unique elements placement

    Unique elements placed at start: 0,1,2,3,4. The array's first 5 elements become [0,1,2,3,4].
  3. Final Answer:

    [0, 1, 2, 3, 4] -> Option A
  4. Quick Check:

    Unique sorted elements remain at front [OK]
Hint: Unique elements stay at front after two-pointer overwrite [OK]
Common Mistakes:
  • Assuming duplicates remain in front
  • Not updating i correctly
  • Confusing array slicing after operation
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