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.
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
}
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
}
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
}
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
}
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
}
Arrays101 Check If N and Its Double Exist in go (0) | 2022.03.02 |
---|---|
Arrays 101 Code from Java to Go(Search in an Array) (0) | 2022.03.02 |
Leetcode Array101 Remove Element in GO (0) | 2022.02.27 |
Arrays 101 Code from Java to Go(Array Deletions) (0) | 2022.02.26 |
Array101 Merge Sorted Array in GO (0) | 2022.02.25 |
댓글 영역