Advertisement

Remove Duplicates from Sorted Array II - LeetCode 80 Solution

Remove Duplicates from Sorted Array II - Complete Solution Guide

Remove Duplicates from Sorted Array II is LeetCode problem 80, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer array nums sorted in non-decreasing order , remove some duplicates in-place such that each unique element appears at most twice . The relative order of the elements should be kept the same . Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums . More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. I

Detailed Explanation

The problem requires you to remove duplicate elements from a sorted integer array `nums` in-place, such that each unique element appears at most twice. The goal is to modify the original array so that the first `k` elements contain the desired result, and then return `k`. Elements beyond `k` don't matter. We are given that the array is sorted in non-decreasing order and we need to do this with O(1) extra space.

Solution Approach

The provided solution uses a single pass through the array with a 'k' pointer that represents the index of the next available position in the modified array. For each element, it checks if `k < 2` (to handle the first two occurrences of any number) or if the current number is greater than the number two positions behind 'k' (`nums[k - 2]`). If either condition is true, it means the current number should be included, so it's written to `nums[k]` and 'k' is incremented. This approach efficiently maintains the required order while ensuring no more than two occurrences of each element.

Step-by-Step Algorithm

  1. Step 1: Initialize `k = 0`. `k` will track the index of the next valid element to be placed in the modified array.
  2. Step 2: Iterate through the input array `nums` using a `for` loop (or equivalent).
  3. Step 3: For each element `num` in `nums`, check if `k < 2` or `num > nums[k - 2]`.
  4. Step 4: If the condition in Step 3 is true, it means the current number `num` should be included in the modified array.
  5. Step 5: If Step 4 is true, then write `num` to `nums[k]`.
  6. Step 6: Increment `k` by 1 after writing `num` to `nums[k]`.
  7. Step 7: After iterating through all elements in `nums`, return `k`. `k` represents the number of valid elements in the modified array, as well as the effective length of the result.

Key Insights

  • Insight 1: The sorted nature of the array is crucial, allowing us to use a two-pointer approach to efficiently identify and overwrite elements in-place.
  • Insight 2: We only need to check if the current element is greater than the element two positions before the current write position (`k - 2`). This condition determines whether to accept the current element for inclusion.
  • Insight 3: The 'k' variable effectively tracks the index where the next non-duplicate (or acceptable duplicate) element should be written to.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Two Pointers.

Companies

Asked at: Accolite, Adobe, Amazon, Apple, Bloomberg, Meta, Microsoft, Nykaa, TikTok, Uber, Yahoo, Yandex, Zoho, tcs.