@@ -0,0 +1,2724 @@
+%22%22%22%0APlease read this as markdown:%0A%0A# Algorithm description%0A%0AProblem statement: https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/727/%0A%0AThe algorithm implemented is as follows:%0A1. Check for two base cases:%0A    1. When %60nums%60 is empty%0A    2. When %60nums%60 length is 1%0A    In both cases return the length of the list because there cannot be%0A    repeated elements.%0A2. Third case: is the length of %60nums%60 is greater than 1:%0A    Traverse the %60nums%60 list comparing the current element vs the previous%0A    element. If those elements are different, move the current element to the%0A    next in-order position %60target_index%60.%0A%0A    %60target_index%60 is the variable we use to keep track of the latest%0A    index that holds ordered, unique elements.%0A%0A# Complexity%0A%0A## Time%0A%0AThe time complexity is O(n). Why?%0A%0A* Travers: we traverse the given %60nums%60 list only once -%3E O(n)%0A* Swap: the %60swap%60 method complexity is O(1).%0A* Simple operation: the other operations are just boolean comparations,%0A  counter increases and variable assignments -%3E O(1).%0A%0ASo we have: O(n) (traverse) * O(1) (swap) * O(1) (simple operations) = O(n)%0A%0A## Space%0A%0AThe space complexity is O(n). Why?%0A%0AThe only data structure we used to solve the problem was the list to hold%0Athe given input %60nums%60. -%3E O(n)%0A%0A## To improve%0A%0AThe problem statement required to:%0A%3E Do not allocate extra space for another array, you must do this by modifying%0A  the input array in-place with O(1) extra memory.%0A%0AIf this constraint was not in place, I would used a dictionary to get the%0Aunique elements present in %60nums%60 and then would have returned the keys of the%0Adictionary in ascending order.%0A%0AThe time complexity would have been the same, the space complexity would have%0Abeen O(n) (given input) + O(n) (dictionary) ~= O(n). But the maintainability%0Aof the code would have improved because the algorithm would be easier to%0Aunderstand and modify by others.%0A%0A%22%22%22%0Aclass Solution:%0A%0A%0A    def switch(self, source_index: int, target_index: int, nums: %5Bint%5D) -%3E None:%0A        nums%5Bsource_index%5D, nums%5Btarget_index%5D = nums%5Btarget_index%5D, nums%5Bsource_index%5D%0A%0A%0A    def removeDuplicates(self, nums: %5Bint%5D) -%3E int:%0A        if len(nums) %3C= 1:%0A            return len(nums)%0A        target_index = 0%0A        previous_number = nums%5B0%5D%0A        for current_index, current_number in enumerate(nums):%0A            if previous_number != current_number:%0A                target_index += 1%0A                self.switch(current_index, target_index, nums)%0A            previous_number = current_number%0A        unique_length = target_index + 1%0A        return unique_length%0A%0A%0Aif __name__ == %22__main__%22:%0A    s = Solution()%0A    r = s.removeDuplicates(%5B0,0,1,1,1,2,2,3,3,4%5D)%0A    print(f'result: %7Br%7D')%0A

Add solution for: Remove Duplicates from Sorted Array