Advertisement

Remove Element - LeetCode 27 Solution

Remove Element - Complete Solution Guide

Remove Element is LeetCode problem 27, a Easy 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 and an integer val , remove all occurrences of val in nums in-place . The order of the elements may be changed. Then return the number of elements in nums which are not equal to val . Consider the number of elements in nums which are not equal to val be k , to get accepted, you need to do the following things: Change the array nums such that the first k elements of nums contain the elements which are not equal to val . The remaining elements of nums are not important

Detailed Explanation

This problem asks us to modify an integer array, `nums`, in-place. Our goal is to 'remove' all occurrences of a specific integer, `val`. The twist is that we're not actually shrinking the array's physical size. Instead, we need to shift all elements *not* equal to `val` to the beginning of the array. The specific requirement is that if there are `k` such elements, the first `k` positions of `nums` must contain them, and the values in `nums` beyond `k` don't matter. Finally, we must return this count `k`.

Solution Approach

The elegance of the provided solution lies in its efficient use of a single-pass, two-pointer approach, albeit a slightly specialized one. We essentially use one pointer, `i`, to traverse the entire original array from left to right, acting as our 'read' pointer. A second pointer, `k`, serves as our 'write' pointer. `k` only advances when we encounter an element (`nums[i]`) that is *not* equal to `val`. When such an element is found, we copy it to `nums[k]` and then increment `k`. If `nums[i]` *is* equal to `val`, we simply skip it. By doing this, `k` effectively keeps track of the next available position for an element that should be kept, ensuring all non-`val` elements are continuously compacted to the beginning of the array. The elements equal to `val` are implicitly 'removed' by being overwritten later by valid elements, or simply left in the irrelevant tail portion of the array.

Step-by-Step Algorithm

  1. Step 1: Initialize a counter `k` to 0. This variable will keep track of the index where the next element different from `val` should be placed.
  2. Step 2: Iterate through the input array `nums` using a loop (from index 0 to `nums.length -1`).
  3. Step 3: Inside the loop, check if the current element `nums[i]` is not equal to `val`.
  4. Step 4: If `nums[i]` is not equal to `val`, copy its value to `nums[k]`. Then, increment `k` to point to the next available position for elements different from `val`.
  5. Step 5: After the loop completes, `k` will hold the number of elements in the array that are not equal to `val`. Return `k`.

Key Insights

  • **In-place compaction via a write pointer:** The core idea is to maintain a `k` pointer that tracks the *next available position* for an element that should be kept. This pointer only increments when a valid element is encountered and written, effectively compacting the array to the left.
  • **Leveraging 'order may be changed' for simplicity:** Since the relative order of elements not equal to `val` doesn't need to be preserved, we don't need complex shifting. We can simply overwrite the 'removed' elements with valid ones found later in the array, making the single-pass copy operation straightforward.
  • **Single-pass linear scan:** The solution processes each element of the array exactly once with the `i` pointer, and each kept element is written at most once by the `k` pointer. This results in an optimal O(N) time complexity, making it highly efficient for large inputs.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Two Pointers.

Companies

Asked at: Accenture, Adobe, Amazon, Apple, Bloomberg, Meta, Microsoft, Uber, Yahoo, Yandex.