Skip to content

Commit b475700

Browse files
author
Saidev
committed
Added "02-Remove Duplicates from Sorted Array.py"
1 parent 0c674e7 commit b475700

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
Example 1:
3+
4+
Input: nums = [1,1,2]
5+
Output: 2, nums = [1,2,_]
6+
Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
7+
It does not matter what you leave beyond the returned k (hence they are underscores).
8+
Example 2:
9+
10+
Input: nums = [0,0,1,1,1,2,2,3,3,4]
11+
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
12+
Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
13+
It does not matter what you leave beyond the returned k (hence they are underscores).
14+
'''
15+
16+
def removeDuplicates(nums):
17+
a, b = 0, 1
18+
while b != len(nums):
19+
if nums[a] != nums[b]:
20+
nums[a+1] = nums[b]
21+
a += 1
22+
b += 1
23+
return a
24+
25+
nums = [1, 1, 2]
26+
print(removeDuplicates(nums))

0 commit comments

Comments
 (0)