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: Use a two-pointer technique to modify the list in place, maintaining the order and uniqueness of elements.
1) If the list is empty, return 0.
2) Initialize a pointer j to 1.
3) Iterate through the list from the second element:
a) If the current element is different from the previous one, place it at the j-th position.
b) Increment j.
4) Trim all elements after the j-th index.
5) Return j as the new length of the list.
⚠️ Common Mistakes
def remove_duplicates(nums):
if not nums:
return 0
# Pointer j for the position of the next unique element
j = 1
# Iterate through the array with i
for i in range(1, len(nums)):
if nums[i] != nums[i-1]:
nums[j] = nums[i]
j += 1
nums[:] = nums[:j]
return j # The new length after removing duplicates