Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Removing Duplicates from Sorted Array II Using Two Pointers

Tech May 10 4

Problem Statement

Given a sorted integer array nums, remove duplicates in-place sothat each unique element appears no more than twice. Return the new length of the array.

Key constraints:

  • Must modify the input array in-place
  • Space complexity must be O(1)
  • The function receives the array by reference

Examples:

Input: nums = [1,1,1,2,2,3]
Output: 5, nums = [1,1,2,2,3]

Input: nums = [0,0,1,1,1,1,2,3,3]
Output: 7, nums = [0,0,1,1,2,3,3]

Solusion Approach

This problem extends the classic "remove duplicates" challenge by allowing each element to appear at most twice instead of once. The two-pointer technique provides an elegant solution.

The core strategy involves two indices:

  • read_ptr scans through the original array
  • write_ptr marks the position where the next valid element should be written

The critical insight is checking whether placing an element would create a third occurrence. By comparing the current element with nums[write_ptr - 2], we determine if this would exceed the two-occurrence limit.

If nums[read_ptr] != nums[write_ptr - 2], the element is valid and gets written to position write_ptr, which then increments.

Implementation

def remove_extra_duplicates(arr):
    """
    Remove duplicates in-place, keeping at most two occurrences.
    Returns the new length of the modified array.
    """
    if len(arr) <= 2:
        return len(arr)
    
    write_idx = 2
    
    for read_idx in range(2, len(arr)):
        if arr[read_idx] != arr[write_idx - 2]:
            arr[write_idx] = arr[read_idx]
            write_idx += 1
    
    return write_idx


# Test cases
def test_solution():
    test1 = [1, 1, 1, 2, 2, 3]
    len1 = remove_extra_duplicates(test1)
    print(f"Length: {len1}, Array: {test1[:len1]}")
    
    test2 = [0, 0, 1, 1, 1, 1, 2, 3, 3]
    len2 = remove_extra_duplicates(test2)
    print(f"Length: {len2}, Array: {test2[:len2]}")


test_solution()

Output:

Length: 5, Array: [1, 1, 2, 2, 3]
Length: 7, Array: [0, 0, 1, 1, 2, 3, 3]

Complexity Analysis

Metric Value Explanation
Time O(n) Single pass through the array
Space O(1) Only two integer variables used

Key Points

The algorithm maintains the relative order of elements while performing in-place modification. The pointer write_idx always points to the next available slot for writing, and elements beyond the returned length should be ignored by the caller.

For arrays with 2 or fewer elements, the function returns immediately since no duplicates can exist beyond the allowed threshold.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.