-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveDuplicatesFromSortedArray.py
More file actions
56 lines (48 loc) · 1.24 KB
/
removeDuplicatesFromSortedArray.py
File metadata and controls
56 lines (48 loc) · 1.24 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
#Author : Yuan Wang
#Date : 2018-05-26
#**********************************************************************************
#
# Given a sorted array, remove the duplicates in place such that each element appear
# only once and return the new length.
#
# Do not allocate extra space for another array, you must do this in place with constant memory.
#
# For example,
# Given input array A = [1,1,2],
#
# Your function should return length = 2, and A is now [1,2].
#
#
#**********************************************************************************/
#worked well with less time,accepted by the leetcode.com
def removeDuplicatesA(nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
k=0
for i in range(1,len(nums)):
if nums[i] != nums[k]:
k+=1
nums[k] = nums[i]
#del nums[k+1:len(nums)]
return k+1
#worked well but slow for large array
def removeDuplicatesB(nums):
for i in nums:
repeat=nums.count(i)
if repeat > 1:
for j in range(repeat-1):
nums.remove(i)
return len(nums)
#worked well but slow for large array
def removeDuplicatesC(nums):
for i in nums:
while(nums.count(i)>1):
nums.remove(i)
return len(nums)
A=[1,1,2,3,3,3,4,4,5]
count=removeDuplicatesA(A)
print(A[:count])