From 1fa223b8ca0085f2ce47968f95314144a2369945 Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Wed, 31 Jan 2024 15:59:20 +0100 Subject: [PATCH] Re-solve "Remove Duplicates from Sorted Array" --- src/remove_duplicates_from_sorted_array.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/remove_duplicates_from_sorted_array.py b/src/remove_duplicates_from_sorted_array.py index 548386d..44879f6 100644 --- a/src/remove_duplicates_from_sorted_array.py +++ b/src/remove_duplicates_from_sorted_array.py @@ -1,13 +1,11 @@ class Solution: def removeDuplicates(self, nums: list[int]) -> int: - if not nums: - return 0 - - to_insert = 1 + j = 1 for i in range(1, len(nums)): - if nums[i] != nums[to_insert - 1]: - nums[to_insert] = nums[i] - to_insert += 1 + if nums[i - 1] == nums[i]: + continue + nums[j] = nums[i] + j += 1 - return to_insert + return j