NLeetCode Solutions
ProblemsPatternsStatistics
© 2026 Created by Trịnh Minh Nhật
GitHubLeetCode
Related posts
  • 26. Remove Duplicates from Sorted ArrayEasy
  • 283. Move ZeroesEasy
  • 1. Two SumEasy
  • 14. Longest Common PrefixEasy
  • 49. Group AnagramsMedium

27. Remove Element

Easy
ArrayTwo Pointers
Answered: Feb 20, 2026
View on LeetCode

Problem Description

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:

  • 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.

Custom Judge:

The judge will test your solution with the following code:

    int[] nums = [...]; // Input array
    int val = ...; // Value to remove
    int[] expectedNums = [...]; // The expected answer with correct length.
                                // It is sorted with no values equaling val.

    int k = removeElement(nums, val); // Calls your implementation

    assert k == expectedNums.length;
    sort(nums, 0, k); // Sort the first k elements of nums
    for (int i = 0; i < actualLength; i++) {
        assert nums[i] == expectedNums[i];
    }

If all assertions pass, then your solution will be accepted.

Examples

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

💡 Hints (3)


Solutions

Complexity Analysis

Time Complexity:O(n)

Each element is visited at most once by the left pointer and at most once by the right pointer, resulting in a linear time complexity.

Space Complexity:O(1)

We are modifying the array in-place and using only a constant amount of extra space for the temporary variable.

In this approach, we use two pointers: one starting at the beginning of the array (left) and the other at the end of the array (right). We iterate through the array using the left pointer. If the element at the left pointer is not equal to val, we increment the left pointer. If the element at the left pointer is equal to val, we swap the element at the left pointer with the element at the right pointer and then decrement the right pointer. We continue this process until the left pointer is no longer less than or equal to the right pointer, which means we have removed all occurrences of val from the array.

Complexity Analysis

Time Complexity:O(n)

Each element is visited once, resulting in a linear time complexity.

Space Complexity:O(1)

We are modifying the array in-place and using only a constant amount of extra space for the variable k.

In this approach, we use a variable k as a pointer to keep track of the position for the next element that is not equal to val. While iterating through the array, if we find an element different from val, we move it to position k and increment k by 1. After the loop finishes, k represents the count of elements that satisfy the condition.

Conclusion

ApproachRatingTime ComplexitySpace ComplexityAdvantagesDisadvantages
Two Pointers with Temporary Variable
O(n)O(n)O(n)O(1)O(1)O(1)Minimizes writes when target values are rare (swaps from end)Requires a temporary variable for swapping, does not preserve order
Two Pointers without Temporary Variable
O(n)O(n)O(n)O(1)O(1)O(1)Simplest and most concise code, preserves relative order of non-target elementsMay perform unnecessary writes when no elements match val

The optimal approach for this problem is Solution 2: Two Pointers without Temporary Variable, which achieves O(n)O(n)O(n) time complexity and O(1)O(1)O(1) space complexity. It simply copies non-target elements forward using a single pointer k, resulting in the cleanest and most readable code while preserving element order.


Previous26. Remove Duplicates from Sorted Array
Next49. Group Anagrams
Related posts
  • 26. Remove Duplicates from Sorted ArrayEasy
  • 283. Move ZeroesEasy
  • 1. Two SumEasy
  • 14. Longest Common PrefixEasy
  • 49. Group AnagramsMedium
On this page
16 lines
1class Solution:
2 def removeElement(self, nums: List[int], val: int) -> int:
3 left = 0
4 right = len(nums) - 1
5
6 while left <= right:
7 if nums[left] != val:
8 left += 1 # if the current element is not equal to val, move the left pointer to the right
9 else: # swap the current element with the element at the right pointer and move the right pointer to the left
10 temp = nums[right]
11 nums[right] = nums[left]
12 nums[left] = temp
13
14 right -= 1
15
16 return left
10 lines
1class Solution:
2 def removeElement(self, nums: List[int], val: int) -> int:
3 k = 0
4
5 for i in range(len(nums)):
6 if nums[i] != val:
7 nums[k] = nums[i] # move the current element to the position of k
8 k += 1
9
10 return k