-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_remove_duplicates.py
More file actions
60 lines (45 loc) · 1.91 KB
/
Copy patharray_remove_duplicates.py
File metadata and controls
60 lines (45 loc) · 1.91 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Leetcode #26. Remove Duplicates from Sorted Array
# Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.
# Consider the number of unique elements in nums to be k. After removing duplicates, return the number of unique elements k.
# The first k elements of nums should contain the unique numbers in sorted order. The remaining elements beyond index k - 1 can be ignored.
# Custom Judge:
# The judge will test your solution with the following code:
# int[] nums = [...]; // Input array
# int[] expectedNums = [...]; // The expected answer with correct length
# int k = removeDuplicates(nums); // Calls your implementation
# assert k == expectedNums.length;
# for (int i = 0; i < k; i++) {
# assert nums[i] == expectedNums[i];
# }
# If all assertions pass, then your solution will be accepted.
#[ 2 , 3, 3, 5, 7, 7, 7, 9] --> [2, 3, 5, 7, 9, 7 , 7, 9] -> result = 5
def removeDuplicates(nums: list) -> int :
numslen = len(nums)
if numslen < 2 :
return numslen
unique_ctr = 0
idx = 0
while idx < numslen :
ctr = 0
while idx + ctr < numslen :
if nums[idx] != nums[idx + ctr] :
break
else :
nums[unique_ctr] = nums[idx + ctr]
ctr += 1
idx = idx + ctr
unique_ctr += 1
return unique_ctr
test_list = [
[1],
[2,5] ,
[5, 5, 5 ,5 ,5 ,5, 5],
[3, 4,4,4,4] ,
[2, 3, 4, 5, 7, 7, 7, 9],
[2, 2, 2, 3, 5],
[0, 2, 4, 5, 6, 7, 7 ,7 ,9 ,9 ,9 ,9 ,9],
[0,0,0,0,1,2,3,4,5] ,
[0,0,0,0,0,1,2,3,4,5,5,9,9, 11, 11] ,
]
for t in test_list :
print(f'input list : {t}, result : {removeDuplicates(t)}, out list: {t}')