← All solutionsRemove Duplicates From Sorted Array
March 03, 2025 • Go •array, two pointer • easy
Problem
- Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.
Go Solution
func removeDuplicates(nums[]int) int {
hashMap:= make(map[int]bool)
count:= 0
for _, num := range nums {
if _, exists := hashMap[num]; !exists {
hashMap[num] = true
nums[count] = num
count++
}
}
return count
}
Performance
- Runtime beats: 100%
- Memory beats: 5.75%
Complexity
LeetCode Problem Link