Skip to content

Commit 1fb3e91

Browse files
committed
removing_duplicates
Source: LeetCode
1 parent 89f2798 commit 1fb3e91

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

remove_duplicates.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Learn Python together
2+
3+
'''Given an integer array nums sorted in non-decreasing order, remove
4+
the duplicates in-place such that each unique element appears only once.
5+
The relative order of the elements should be kept the same.'''
6+
7+
# Solution
8+
def removeDuplicates(nums):
9+
if not nums:
10+
return 0
11+
k = 0
12+
for i in range(len(nums)):
13+
# check if the current value isn't equal to the value at index k
14+
if nums[i] != nums[k]:
15+
k += 1
16+
# update the element at index k to be the current element
17+
nums[k] = nums[i]
18+
return k + 1 # return the count of unique elements
19+
20+
list1 = [0,0,1,1,1,2,2,3,3,4]
21+
list2 = [1,1,2,]
22+
list3 = []
23+
print(removeDuplicates(list1)) # Output -> 5
24+
print(removeDuplicates(list3)) # Output -> 0
25+
print(removeDuplicates(list2)) # Output -> 2
26+
27+

0 commit comments

Comments
 (0)