-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveElement.py
More file actions
39 lines (30 loc) · 799 Bytes
/
removeElement.py
File metadata and controls
39 lines (30 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def removeElement(nums: list[int], val: int) -> int:
ptr1 = 0
ptr2 = 0
k = len(nums)
while ptr2 < len(nums):
if nums[ptr2] == val:
ptr2 += 1
k -= 1
else:
temp = nums[ptr1]
nums[ptr1] = nums[ptr2]
nums[ptr2] = temp
ptr1 += 1
ptr2 += 1
return k
def removeDuplicates(nums: List[int]) -> int:
ptr1 = 1
ptr2 = 1
if len(nums) == 1:
return 1
for i in range(1, len(nums)):
if nums[i] == nums[i-1]:
ptr1 += 1 # find the next insert point
else:
nums[ptr2] = nums[ptr1]
ptr2 += 1
ptr1 += 1
return ptr2
myList = [3, 2, 2, 3]
removeElement(myList, 3)