Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    0027 - Remove Element
    0027 - Remove Element

    0027 - Remove Element

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

    Solution 1 - Two Pointer (Overwrite from Left)

    Use a slow pointer k to track the position of the next non-val element. Iterate through the array with a fast pointer. Whenever the current element is not equal to val, copy it to position k and increment k. Return k as the new length.

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

    Solution 2 - Two Pointer (Swap with End)

    Use two pointers starting from both ends of the array. When the left pointer finds a val, swap it with the element at the right pointer and shrink the right boundary. This avoids unnecessary copies when val 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 (Overwrite from Left) """
    
    #####################################################################################
    # 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 - Two Pointer (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"""
            left = 0
            right = len(nums) - 1
    
            while left <= right:
                if nums[left] == val:
                    nums[left] = nums[right]
                    right -= 1
                else:
                    left += 1
    
            return left
    
    
    #####################################################################################
    # 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, 4, 0, 3, ...]
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()