Unit 4 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Iterate through the array with a fast pointer to examine each element. Use a slow pointer to place elements that are not equal to the target value.
1) Initialize a slow pointer `i` at the start of the array.
2) For each element in the array (`j` as the fast pointer):
a) If the element at `j` is not equal to `val`:
i) Place the element at the slow pointer's current position.
ii) Increment the slow pointer `i`.
3) Return the position of the slow pointer, which indicates the new length of the array.
⚠️ Common Mistakes
def removeElement(nums, val):
i = 0 # Start from the first element
for j in range(len(nums)):
if nums[j] != val:
nums[i] = nums[j]
i += 1
return i