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
Constraints
- ●
0 <= nums.length <= 100 - ●
0 <= nums[i] <= 50 - ●
0 <= val <= 100
Examples
nums = [3,2,2,3], val = 32, nums = [2,2,_,_]nums = [0,1,2,2,3,0,4,2], val = 25, nums = [0,1,4,0,3,_,_,_]Asked At
2 companies| Company | Difficulty | |
|---|---|---|
| Amazon | Easy | View all Amazon questions → |
| Bloomberg | Easy | View all Bloomberg questions → |
How to Think About It
Brute force: shift elements left every time you find val, causing O(n^2) time.
Use two pointers: one for scanning and one for writing the next valid element.
If the current element equals val, skip it; otherwise write it to the write pointer.
The write pointer always points to the next position for a valid element.
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
What Trips People Up in Real Interviews
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.
Solution Code
O(n) timedef removeElement(nums, val):
write = 0
for num in nums:
if num != val:
nums[write] = num
write += 1
return writeFrequently 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.