Skip to content
LC-0027 Easy LeetCode

27. Remove Element

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 60% Topics: Array, Two Pointers
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    # @param    A       a list of integers
    # @param    elem    an integer, value need to be removed
    # @return an integer
    def removeElement(self, A, elem):
        i, last = 0, len(A) - 1
        while i <= last:
            if A[i] == elem:
                A[i], A[last] = A[last], A[i]
                last -= 1
            else:
                i += 1
        return last + 1

Solution from kamyu104/LeetCode-Solutions · MIT