Meta Data#
Difficulty: easy First Attempt: 2022-05-11
- Submissions: Wrong Answer → Accepted ×3
Approach#
A write pointer k (Two Pointers Template 5): copy every value not equal to val to nums[k] and advance. The first k slots are what remains; order does not matter.
My Attempts#
Attempt 1 compared v != nums (a number against the whole list), always true, so nothing was removed: [2], val = 2 returned 1.
Solution#
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
k = 0
for x in nums:
if x != val:
nums[k] = x
k += 1
return k
Complexity#
Time O(n), space O(1).
Mistakes to Avoid#
- Compare against
val.
Interview Explanation#
A write pointer copies values not equal to val to the front in order, and I return the count. Time O(n), space O(1).
