Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0027 - Remove Element
    0027 - Remove Element
    0027 - Remove Element
    0027 - Remove Element

    0027 - Remove Element

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/remove-element/

    Solution 1 - Two Pointer (In-Place)

    Use a slow pointer k to track the write position. Iterate through the array with a fast pointer i. Whenever nums[i] is not equal to val, copy it to nums[k] and increment k. After the loop, k is the count of elements not equal to val.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)

    Solution 2 - Swap with End

    When encountering an element equal to val, swap it with the last element and shrink the array size. This avoids unnecessary shifts when the element to remove is rare.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)
    """ 0027.1 - Remove Element - Solution 1 - Two Pointer (In-Place) """
    
    #####################################################################################
    # Imports
    #####################################################################################
    from typing import List
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def removeElement(self, nums: List[int], val: int) -> int:
            """Remove Element Function"""
            k = 0
            for i in range(len(nums)):
                if nums[i] != val:
                    nums[k] = nums[i]
                    k += 1
            return k
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        nums1 = [3, 2, 2, 3]
        print(Solution().removeElement(nums1, 3), nums1)  # 2, [2, 2, ...]
    
        nums2 = [0, 1, 2, 2, 3, 0, 4, 2]
        print(Solution().removeElement(nums2, 2), nums2)  # 5, [0, 1, 3, 0, 4, ...]
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0027.2 - Remove Element - Solution 2 - Swap with End """
    
    #####################################################################################
    # Imports
    #####################################################################################
    from typing import List
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def removeElement(self, nums: List[int], val: int) -> int:
            """Remove Element Function"""
            i = 0
            n = len(nums)
            while i < n:
                if nums[i] == val:
                    nums[i] = nums[n - 1]
                    n -= 1
                else:
                    i += 1
            return n
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        nums1 = [3, 2, 2, 3]
        print(Solution().removeElement(nums1, 3), nums1)  # 2, [2, 2, ...]
    
        nums2 = [0, 1, 2, 2, 3, 0, 4, 2]
        print(Solution().removeElement(nums2, 2), nums2)  # 5, [0, 1, 3, 0, 4, ...]
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()