diff --git a/leetcode/static_arrays/0026_remove_duplicates_from_sorted_array.go b/leetcode/static_arrays/0026_remove_duplicates_from_sorted_array.go new file mode 100644 index 0000000..046ac85 --- /dev/null +++ b/leetcode/static_arrays/0026_remove_duplicates_from_sorted_array.go @@ -0,0 +1,26 @@ +package main + +import "fmt" + +func removeDuplicates(nums []int) int { + if len(nums) == 1 { + return 1 + } + var unique, rp int = 0, 0 + for i := 0; i < len(nums); i++ { + if nums[rp] != nums[i] { + unique++ + rp++ + nums[rp] = nums[i] + } + } + return unique + 1 +} + +func main() { + nums := []int{1, 1, 2} + // nums := []int{0, 0, 1, 1, 1, 2, 2, 3, 3, 4} + res := removeDuplicates(nums) + fmt.Println(res) + fmt.Println(nums) +} 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 e69de29..bae924c 100644 --- a/leetcode/static_arrays/0026_remove_duplicates_from_sorted_array.py +++ b/leetcode/static_arrays/0026_remove_duplicates_from_sorted_array.py @@ -0,0 +1,21 @@ +from typing import List + +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + if len(nums) <= 1: + return 0 + unique, iteratror, real_pose = 1, 1, 0 + while iteratror < len(nums): + if nums[real_pose] != nums[iteratror]: + unique += 1 + real_pose += 1 + nums[real_pose] = nums[iteratror] + iteratror += 1 + return unique + + +if __name__ == "__main__": + # nums = [1, 1, 2] + nums = [0,0,1,1,1,2,2,3,3,4] + print(Solution().removeDuplicates(nums)) + print(nums)