0
0
DSA Pythonprogramming~10 mins

Remove Duplicates from Sorted Array Two Pointer in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the slow pointer for the two-pointer technique.

DSA Python
def remove_duplicates(nums):
    if not nums:
        return 0
    slow = [1]
    for fast in range(1, len(nums)):
        if nums[fast] != nums[slow]:
            slow += 1
            nums[slow] = nums[fast]
    return slow + 1
Drag options to blanks, or click blank then click option'
A0
B1
C-1
Dlen(nums)
Attempts:
3 left
💡 Hint
Common Mistakes
Starting slow at 1 causes skipping the first element.
Using -1 causes index errors.
2fill in blank
medium

Complete the code to check if the current fast pointer element is different from the slow pointer element.

DSA Python
def remove_duplicates(nums):
    if not nums:
        return 0
    slow = 0
    for fast in range(1, len(nums)):
        if nums[fast] [1] nums[slow]:
            slow += 1
            nums[slow] = nums[fast]
    return slow + 1
Drag options to blanks, or click blank then click option'
A!=
B<
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' causes duplicates to be copied.
Using '<' or '>' is incorrect for equality check.
3fill in blank
hard

Fix the error in the code to correctly update the slow pointer and assign the new unique element.

DSA Python
def remove_duplicates(nums):
    if not nums:
        return 0
    slow = 0
    for fast in range(1, len(nums)):
        if nums[fast] != nums[slow]:
            [1]  # increment slow pointer
            nums[slow] = nums[fast]
    return slow + 1
Drag options to blanks, or click blank then click option'
Afast += 1
Bslow = fast
Cslow += 1
Dslow -= 1
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning slow = fast skips elements.
Incrementing fast inside loop causes errors.
4fill in blank
hard

Fill both blanks to complete the function that returns the length of the array after removing duplicates.

DSA Python
def remove_duplicates(nums):
    if not nums:
        return 0
    slow = 0
    for fast in range(1, len(nums)):
        if nums[fast] != nums[slow]:
            slow [1] 1
            nums[slow] = nums[fast]
    return slow [2] 1
Drag options to blanks, or click blank then click option'
A+=
B-=
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-=' decreases slow incorrectly.
Returning slow - 1 gives wrong length.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each unique number to its index after removing duplicates.

DSA Python
def map_unique_indices(nums):
    length = remove_duplicates(nums)
    return { [1]: [2] for [3] in range(length) }
Drag options to blanks, or click blank then click option'
Anums[i]
Bi
Dnum
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'num' causes NameError as 'num' is not defined.
Mixing variable names like using 'i' for key maps index to index.