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.
Performance
Runtime beats: 100%
Memory beats: 5.75%
Complexity
Time:O(n)
Space:O(n)
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
}