Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0026 - Remove Duplicates from Sorted Array
    0026 - Remove Duplicates from Sorted Array
    0026 - Remove Duplicates from Sorted Array
    0026 - Remove Duplicates from Sorted Array

    0026 - Remove Duplicates from Sorted Array

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/remove-duplicates-from-sorted-array/

    Solution 1 - Two Pointer (In-Place)

    Use a slow pointer k to track the position of the next unique element. Iterate with a fast pointer i through the array. Whenever nums[i] differs from nums[k], increment k and overwrite nums[k] with nums[i].

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

    Solution 2 - Using Set with Sort

    Convert the array to a set to remove duplicates, sort the result, and copy the unique values back into the original array. Return the count of unique elements.

    TimeComplexity:O(n⋅log⁡n)TimeComplexity: O(n \cdot \log n)TimeComplexity:O(n⋅logn)
    SpaceComplexity:O(n)SpaceComplexity: O(n)SpaceComplexity:O(n)
    """ 0026.1 - Remove Duplicates from Sorted Array - Solution 1 - Two Pointer (In-Place) """
    
    #####################################################################################
    # Imports
    #####################################################################################
    from typing import List
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def removeDuplicates(self, nums: List[int]) -> int:
            """Remove Duplicates from Sorted Array Function"""
            if not nums:
                return 0
            k = 0
            for i in range(1, len(nums)):
                if nums[i] != nums[k]:
                    k += 1
                    nums[k] = nums[i]
            return k + 1
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        nums1 = [1, 1, 2]
        print(Solution().removeDuplicates(nums1), nums1)  # 2, [1, 2, ...]
    
        nums2 = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
        print(Solution().removeDuplicates(nums2), nums2)  # 5, [0, 1, 2, 3, 4, ...]
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0026.2 - Remove Duplicates from Sorted Array - Solution 2 - Using Set with Sort """
    
    #####################################################################################
    # Imports
    #####################################################################################
    from typing import List
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def removeDuplicates(self, nums: List[int]) -> int:
            """Remove Duplicates from Sorted Array Function"""
            unique = sorted(set(nums))
            for i in range(len(unique)):
                nums[i] = unique[i]
            return len(unique)
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        nums1 = [1, 1, 2]
        print(Solution().removeDuplicates(nums1), nums1)  # 2, [1, 2, ...]
    
        nums2 = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
        print(Solution().removeDuplicates(nums2), nums2)  # 5, [0, 1, 2, 3, 4, ...]
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()