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]
    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.

Subscribe via email

Get notified once/twice per month when new articles are published.

Copyright © 2022 - 2024 TheDeveloperCafe.
The Go gopher was designed by Renee French.