-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path26.py
More file actions
26 lines (24 loc) · 984 Bytes
/
26.py
File metadata and controls
26 lines (24 loc) · 984 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
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
# brute-force approach
# time complexity: O(n^2)
# because each element is compared with the other element to check for duplicates.
# i = 0
# while i < len(nums) - 1:
# if nums[i] == nums[i + 1]:
# nums.pop(i + 1)
# else:
# i += 1
# return len(nums)
# T.C = O(n)
# input length is empty, return 0
if len(nums) == 0:
return 0
# initialize a pointer at i, to track the element
i = 0
# loop through the list starting at i + 1 with pointer j
for j in range(i + 1, len(nums)):
if nums[j] != nums[i]: # if element at j is different from the element at i
i += 1 # increment i and set i to nums[j]
nums[j] = nums[i]
return [i + 1] # return length of the new list, since i, is zero-indexed