Skip to content

Commit 8ceb9f4

Browse files
committed
remove_duplicates_from_sorted_array_26: solved
1 parent ab45e53 commit 8ceb9f4

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 26. Remove Duplicates from Sorted Array
2+
3+
https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package remove_duplicates_from_sorted_array_26
2+
3+
func removeDuplicates(nums []int) int {
4+
i, j, u := 0, 0, 1
5+
for j < len(nums)-1 {
6+
j = i
7+
for j < len(nums)-1 && nums[i] == nums[j] {
8+
j++
9+
}
10+
if nums[i] != nums[j] {
11+
u++
12+
}
13+
14+
for k := j - 1; k > i; k-- {
15+
nums[k] = nums[j]
16+
}
17+
18+
i++
19+
}
20+
21+
return u
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package remove_duplicates_from_sorted_array_26
2+
3+
import "testing"
4+
5+
func Test_removeDuplicates(t *testing.T) {
6+
type args struct {
7+
nums []int
8+
}
9+
tests := []struct {
10+
name string
11+
args args
12+
want int
13+
}{
14+
{
15+
name: "remove duplicates",
16+
args: args{nums: []int{1, 1, 2}},
17+
want: 2,
18+
},
19+
{
20+
name: "remove duplicates",
21+
args: args{nums: []int{1, 1}},
22+
want: 1,
23+
},
24+
{
25+
name: "remove duplicates",
26+
args: args{nums: []int{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}},
27+
want: 5,
28+
},
29+
}
30+
for _, tt := range tests {
31+
t.Run(tt.name, func(t *testing.T) {
32+
if got := removeDuplicates(tt.args.nums); got != tt.want {
33+
t.Errorf("removeDuplicates() = %v, want %v", got, tt.want)
34+
}
35+
})
36+
}
37+
}

0 commit comments

Comments
 (0)