Remove Duplicates from Sorted Array
Solution in go
func removeDuplicates(nums []int) int {    
    index := 1
    
    for i := 1; i < len(nums); i++ {
        if nums[i] != nums[index - 1] {
            nums[index] = nums[i]
            index++
        }        
    }
    
    return index
}

index represents the position at which the next insertion will happen. It is set to 1 because we can safely ignore the first element in the nums slice as it will always stay the same.

    TheDeveloperCafe © 2022-2024