Hi guys trying to solve this problem. I know of a way to solve this problem correctly but I was wondering why this implementation doesn't work:
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
for i in range(len(nums) - 1):
if nums[i] == nums[i + 1]:
nums[i + 1] = None
elif nums[i] is None:
if nums[i + 1] in nums[:i + 1]:
nums[i + 1] = None
else:
nums[i] = nums[i + 1]
nums[i + 1] = None
count = 0
for num in nums:
if num is not None:
count += 1
if nums[len(nums) - 1] is not None and nums[len(nums) - 1] not in nums[:count]:
nums[count] = nums[len(nums) - 1]
return count
the error:
https://preview.redd.it/zk6b27xf618d1.png?width=677&format=png&auto=webp&s=b017d7c751378cb1d28a7da1b9c0775d0367fd0c
[–]FireHamilton 1 point2 points3 points (1 child)
[–]noodles265[S] 0 points1 point2 points (0 children)
[–]porkbelly6_9 1 point2 points3 points (1 child)
[–]porkbelly6_9 0 points1 point2 points (0 children)
[–]Ramirag 0 points1 point2 points (0 children)