EasyArrayTwo PointersUpdated Sep 2026

27. Remove Element

Asked at Amazon, Bloomberg

Difficulty

Easy

Time

O(n)

Space

O(1)

Pattern

Array

Companies

2

Problem

You get an integer array nums and an integer val. Delete every element of nums that equals val, and do it in place — you must change the given array itself, not build a new one.

After your work, the first k slots of nums must hold exactly the elements that are not equal to val. The order of those elements does not matter. Whatever sits in the slots after position k is ignored, and the array's length never changes.

Return k, the count of elements that survived. The judge sorts the first k slots of your array and compares them with the expected values, so only those first k slots need to be correct.

Keep the non-val elements at the front

nums32230123val = 3, so the two 3s (red) are removednums afterk = 222__0123The first k = 2 slots hold the survivors; the rest isignored
Elements equal to val must go; the survivors move to the front and k counts them.

Constraints

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

Examples

Example 1
Inputnums = [3,2,2,3], val = 3
Output2, nums = [2,2,_,_]
WhyTwo elements are not 3, so k = 2. Both of them are 2, and they now sit in the first two slots. The last two slots are leftovers and do not matter.
Example 2
Inputnums = [0,1,2,2,3,0,4,2], val = 2
Output5, nums = [0,1,4,0,3,_,_,_]
WhyThree of the eight elements equal 2, so 5 remain and k = 5. The first five slots hold 0, 1, 4, 0, 3 in some order; the three slots after them are ignored.

Asked At

2 companies

How to Think About It

1

Brute force: shift elements left every time you find val, causing O(n^2) time.

2

Use two pointers: one for scanning and one for writing the next valid element.

3

If the current element equals val, skip it; otherwise write it to the write pointer.

4

The write pointer always points to the next position for a valid element.

5

After the scan, the write pointer equals the new length.

Optimal Approach

Maintain a write pointer initialized to zero. Iterate through the array with a read pointer. Whenever the read pointer encounters a value not equal to val, copy it to the write pointer position and increment the write pointer. At the end, the write pointer is the new length of the modified array. This runs in O(n) time with O(1) space.

Read pointer and write pointer

nums32230123readwriteStart: both pointers at index 0, nothing written yetnums22230123readwriteThe 3 is skipped; each 2 is copied to the write slotand write moves onif nums[read] != val: nums[write] = nums[read] write += 1return write
The read pointer scans every element; the write pointer marks where the next survivor goes.
ComplexityTime O(n)Space O(1)We scan the array once with a read pointer, and the write pointer only moves forward, so no extra array is needed.

What Trips People Up in Real Interviews

1

Confirm the order of remaining elements does not matter.

2

Ask if you must do it in O(1) extra space (yes, in-place).

3

Explain the two-pointer approach clearly before coding.

4

Handle edge cases: all elements are val, no elements are val.

5

Mention that swapping approach also works and avoids shifts.

Solution Code

O(n) time
def removeElement(nums, val):
    write = 0
    for num in nums:
        if num != val:
            nums[write] = num
            write += 1
    return write

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Remove Element problem?

Given an integer array nums and an integer val, remove all occurrences of val in-place and return the new length of the array. The relative order of elements may change.

How do you solve Remove Element?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Remove Element?

Remove Element is asked at Amazon, Bloomberg. It is a easy difficulty problem.

What are common mistakes on Remove Element?
  • Confirm the order of remaining elements does not matter.
  • Ask if you must do it in O(1) extra space (yes, in-place).
  • Explain the two-pointer approach clearly before coding.
  • Handle edge cases: all elements are val, no elements are val.
  • Mention that swapping approach also works and avoids shifts.