상세 컨텐츠

본문 제목

Array101 Remove Duplicates from Sorted Array in GO

Go/Leet Code

by Gopythor 2022. 3. 1. 18:03

본문

728x90
반응형

Accepted Solutions Runtime Distribution
Accepted Solutions Memory Distribution

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.
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.
Return k after placing the final result in the first k slots of nums.
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
Custom Judge:
The judge will test your solution with the following code:
Constraints:
1 <= nums.length <= 3 * 104
-100 <= nums[i] <= 100
nums is sorted in non-decreasing order.

Code

func removeDuplicates(nums []int) int {
    if len(nums) == 0 {
        return 0
    }
    ch := make(chan int)
    go checker(nums, ch)
    k := <-ch
    return k
}

func checker(nums []int, ch chan int) {
    k := 0
    n := -101
    for _, v := range nums {
        if n != v {
            nums[k] = v
            k++
            n = v
        }
    }
    ch <- k
}
  • Because I got impression from last code which saw example last time.
  • So I wanted to imitate that code without checking again but just remembering the logic.
  • That code was used for skipping designated number.
  • I initiated checker variable as -101 because constrains showed presented number is bigger than -101.
  • First, v(nums[0]) will be checked with n, but n will have different number.
  • v will be allocated to nums(k) actually nums(0) at first.
  • and increment will work to k.
  • As a result n will have nums[0] value and for loop will be continued.
  • To reduce process time, I implemented Go routine.
    https://go.dev/play/p/V15XPOiTU2J

Sample 0 ms submission

func removeDuplicates(nums []int) int {
    i, j := 0, 0
    for j < len(nums) {
        if nums[i] == nums[j] {
            j++
        } else {
            nums[i + 1] = nums[j]
            i++
        }
    }
    return i + 1
}
  • When number is duplicated while looping, i will stay.
  • But j found not duplicated one, new one will be allocated to i+1 which is next array had duplicated one.
  • i indicated array which allocated after cheking duplicated.
  • So it will be return numbers which has.
  • somehow complicated.

sample 2 ms submission

func removeDuplicates(nums []int) int {
    k := 0
    for i:=0; i < len(nums)-1; i++ {
        if nums[i] == nums[i + 1] {
            continue
        } else {
            k++
            temp := nums[i + 1]
            nums[k] = temp
        }
    }
    return k + 1
}
  • Because this code uses i+1 when comparing numbers, for loof condition was set as len(nums)-1
  • I don't think temp is needed.. It works without temp.

sample 4300 KB submission

func removeDuplicates(nums []int) int {
    last_index := 0
    for i:=0; i < len(nums); i++ {
        if i == 0 || nums[i] != nums[i-1] {
            nums[last_index] = nums[i]
            last_index++
        }
    }
    return last_index
}
  • In If condition when for loop starts, nums(0) will be in last index(nums(last_index) == nums(0))
  • So it starts from 1 actually.
  • It compares i with i-1 so len(nums) is used.
  • when different number is detected compared to i-1, num will be allocated to last_index and it will increment.

sample 4400 KB submission

func removeDuplicates(nums []int) int {
    cur_value:=nums[0]
    new_index:=1
    index:=1

    for index <len(nums){
        if nums[index] != cur_value {
            nums[new_index]=nums[index]
            cur_value=nums[new_index]
            new_index++
        }
        index++
    }
    return new_index
}
  • This code starts from array(1) becuase initial values of array(0) is set.
728x90
반응형

관련글 더보기

댓글 영역