[LeetCode-Easy] Remove Duplicates from Sorted Array

Understand How To Solve This Problem Using Golang And Javascript.

Javascript

var removeDuplicates = function(nums) {
    if (nums.length == 0){
    return 0        
    } 
    let i = 0
    
    for (let s = 1; s < nums.length; s++) {
        if(nums[s] != nums[i]) {
            i++
            nums[i] = nums[s]
        }
    }
    return i + 1
    
};

Golang

func removeDuplicates(nums []int) int {
    if len(nums) == 0 {
        return 0
    }
    
    c := 0
    for i := 1; i < len(nums); i++ {
        if nums[c] != nums[i] {
            c++
            nums[c] = nums[i]    
        }
    }
    return c + 1
}

c 設定為當前的位置

檢查當前的值 是否和下一位數一致

不一致就讓 c++

再讓加完後 c 的位置的值等於 loop 到的值