# A python method, room for clarification?

• ``````class Solution:
# @param    A       a list of integers
# @param    elem    an integer, value need to be removed
# @return an integer
def removeElement(self,A, elem):
if len(A)<=0:
return 0

end=len(A)-1
first=-1
while A[end]==elem and end>=0:
end-=1

if(end<0):
return 0

while first!=end and end>=0 and first<len(A):
first+=1
if(A[first]==elem):
A[first]=A[end]
A[end]=elem
while A[end]==elem and end>=0:
end-=1

return end+1
``````

looks it has too many if test and too many extra check in the while conditions. Are they necessary? Basically it swaps elem item to where the "end" pointer is and move end, first pointers correspondingly. However there are many cases where they can miss each other.

Ok I changed it so they will definitely meet.,first<len(A) is unnecessary now :

``````class Solution:
# @param    A       a list of integers
# @param    elem    an integer, value need to be removed
# @return an integer
def removeElement(self,A, elem):
if len(A)<=0:
return 0

end=len(A)-1
first=-1

while A[end]==elem and end>=0:
end-=1

if(end<0):
return 0

while first!=end:
first+=1
if(A[first]==elem):
A[first]=A[end]
A[end]=elem
while A[end]==elem and end>=0:
end-=1
if end<0:
break

return end+1``````

• No , All of these checks are not necessary
You need to use two pointers . one of them for the position of writing and the other for reading

``````class Solution:
def removeElement(self, A, elem):