Remove Element-Leetcode 27 Solution

The Remove Element problem is a classic LeetCode challenge. The task is to remove all occurrences of a specified value (val) from an integer array nums in-place and return the count of the remaining elements. Here’s a comprehensive guide to solving the problem, covering all approaches from naive to optimal.

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 as well as the size of nums.
  • Return k.

Example 1:

Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).

Example 2:

Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
Note that the five elements can be returned in any order.
It does not matter what you leave beyond the returned k (hence they are underscores).

Constraints:

  • 0 <= nums.length <= 100
  • 0 <= nums[i] <= 50
  • 0 <= val <= 100

Approach 1: Naive Method

Iterate through the array, and for every match of val, shift all subsequent elements one position to the left.

Code:

class Solution {
    public int removeElement(int[] nums, int val) {
        int n = nums.length;
        for (int i = 0; i < n; ) {
            if (nums[i] == val) {
                for (int j = i; j < n - 1; j++) {
                    nums[j] = nums[j + 1];
                }
                n--; // Reduce the effective size of the array
            } else {
                i++;
            }
        }
        return n;
    }
}

Time Complexity: O(n2)
Space Complexity: O(1)

Approach 2: Two Pointers – Iterative

Use two pointers:

  • One pointer i iterates through the array.
  • Another pointer k keeps track of the position to overwrite.

Code:

class Solution {
    public int removeElement(int[] nums, int val) {
        int k = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val) {
                nums[k++] = nums[i];
            }
        }
        return k;
    }
}

Time Complexity: O(n)
Space Complexity: O(1)

Approach 3: Two Pointers – Optimized

Optimize by swapping elements from the end of the array when val is found, thus avoiding unnecessary overwrites.

Code:

class Solution {
    public int removeElement(int[] nums, int val) {
        int n = nums.length;
        int i = 0;
        while (i < n) {
            if (nums[i] == val) {
                nums[i] = nums[--n];
            } else {
                i++;
            }
        }
        return n;
    }
}

Time Complexity: O(n)
Space Complexity: O(1)

Complexity Analysis

  1. Naive Method: Quadratic runtime makes it unsuitable for larger arrays.
  2. Iterative Two Pointers: Linear time with minimal space, efficient for most scenarios.
  3. Optimized Two Pointers: Reduces unnecessary assignments when val is common.

Summary

The two-pointer approach is the most efficient and is recommended for solving this problem. It achieves optimal runtime and modifies the array in-place without additional memory overhead.

Leave a comment