Remove Duplicates From Sorted Array Ii

To solve this coding challenge, we need to modify the input array
nums
in-place to ensure that each element appears at most twice while maintaining the order of the elements. The goal is to return the length of the modified array where each unique element appears at most twice.

Explanation

  1. Understanding the Problem :
    • The input array
      nums
      is sorted in non-decreasing order.
    • We need to ensure that no element appears more than two times in the modified array.
    • The modification should be done in-place, which means we should not use any additional arrays or storage; instead, we should transform
      nums
      directly.
    • We need to return the length
      k
      of the modified array where elements appear at most twice.
  2. Example Analysis :
    • Example 1: For
      nums = [1,1,1,2,2,3]
      , the output should be
      [1,1,2,2,3, _]
      with
      k = 5
      .
    • Example 2: For
      nums = [0,0,1,1,1,1,2,3,3]
      , the output should be
      [0,0,1,1,2,3,3,_,_]
      with
      k = 7
      .
  3. Approach :
    • If the length of
      nums
      is 2 or less, return the length because such small arrays inherently satisfy the condition.
    • Use a pointer
      count
      starting at 2 (because the first two elements are always allowed).
    • Iterate through the array starting from the third element. If the current element is different from the element
      count - 2
      positions back, write this element to the position indicated by
      count
      .
    • Increment
      count
      whenever we write a new value, ensuring no element appears more than twice.

    Detailed Steps in Pseudocode

  4. Initial Setup :
    • Check if the length of the array is less than or equal to 2. If true, return the length of the array directly.
  5. Using a Count Pointer :
    • Initialize
      count
      to 2.
    • Iterate over the array starting from index 2.
    • If the current element is different from the element at
      count - 2
      , write this element at the
      count
      position.
  6. Iteration Logic :
    • For each element starting from index 2, compare it with the element at
      count - 2
      .
    • Update the element at
      count
      and then increment the
      count
      if the current condition is satisfied.
    • Continue this process until the end of the array is reached.
  7. Return the Result :
    • Return the value of
      count
      which represents the length of the modified array.

    Pseudocode

                                                
    # Function to remove duplicates from sorted array
    function removeDuplicates(nums):
    
    # If the array length is less than or equal to 2, return its length
    if length of nums <= 2:
    return length of nums  # Array is already valid
    
    # Initialize the pointer for placing non-duplicate elements
    count = 2  
    
    # Traverse the array starting from the third element
    for i from 2 to length of nums - 1:
    # Check if the current element is different from the element `count - 2` positions back
    if nums[i] != nums[count - 2]:
    # Place the current element at the position `count`
    nums[count] = nums[i]  
    # Increment the count
    count += 1  
    
    # Return the count, which is the length of the modified array
    return count
    
                                            

    Explanation:

  8. Checking Length :
    • Before proceeding, we check if the length of
      nums
      is less than or equal to 2. If it is, we return the same length of
      nums
      .
  9. Initial Setup :
    • We start with
      count
      set to 2 because we allow the first two elements of the array to remain unchanged.
  10. Main Loop :
    • We iterate from the third element (index 2) to the end of the array.
    • In the loop, for each element at index
      i
      , we compare it with the element at
      count - 2
      .
    • If they are different, it means that this element can be added to our modified array. We place the element at the position
      count
      and then increment
      count
      .
  11. Returning Result :
    • Finally, we return the value of
      count
      , which gives the length of the modified array that meets the criteria.
By following this detailed explanation and pseudocode, we can successfully solve the problem of removing duplicates in a sorted array while ensuring each unique element appears no more than twice.