diff --git a/leetcode/static_arrays/0026_remove_duplicates_from_sorted_array.py b/leetcode/static_arrays/0026_remove_duplicates_from_sorted_array.py index bae924c..50cf834 100644 --- a/leetcode/static_arrays/0026_remove_duplicates_from_sorted_array.py +++ b/leetcode/static_arrays/0026_remove_duplicates_from_sorted_array.py @@ -1,3 +1,4 @@ +# Link to task: https://leetcode.com/problems/remove-duplicates-from-sorted-array/ from typing import List class Solution: diff --git a/leetcode/static_arrays/0027_remove_element.py b/leetcode/static_arrays/0027_remove_element.py new file mode 100644 index 0000000..ab9ea51 --- /dev/null +++ b/leetcode/static_arrays/0027_remove_element.py @@ -0,0 +1,23 @@ +# Link: https://leetcode.com/problems/remove-element/description/ +from typing import List + + +class Solution: + def removeElement(self, nums: List[int], val: int) -> int: + c = 0 + n = len(nums) - 1 + while n >= 0: + if nums[n] == val: + del nums[n] + else: + c += 1 + n -= 1 + return c + + +if __name__ == "__main__": + # nums = [0, 1, 2, 2, 3, 0, 4, 2] + nums = [3, 2, 2, 3] + print(nums) + print(Solution().removeElement(nums, 3)) + print(nums)