Skip to content

Instantly share code, notes, and snippets.

@khomutoff
Last active February 7, 2017 02:16
Show Gist options
  • Select an option

  • Save khomutoff/50d6d2c13bea8b73e3a0c42120844a00 to your computer and use it in GitHub Desktop.

Select an option

Save khomutoff/50d6d2c13bea8b73e3a0c42120844a00 to your computer and use it in GitHub Desktop.
class Solution:
# Given a sorted array, remove the duplicates in place such that each element can appear atmost twice and return the new length.
# @param A : list of integers
# @return an integer
def removeDuplicates(self, A):
if len(A) < 2:
return len(A)
index = 0
while index < (len(A)):
if A[index] == A[index - 2]:
A.pop(index)
else:
index += 1
return len(A)
class Solution:
# @param A : list of integers
# @return an integer
def removeDuplicates(self, A):
if len(A) < 4:
return len(A)
index = 1
match = False
while index < (len(A)):
if A[index] == A[index - 1]:
if match is True:
A.pop(index)
else:
match = True
index += 1
else:
match = False
index += 1
return len(A)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment