Skip to content

Commit 645f239

Browse files
committed
remove_duplicates_from_sorted_array
1 parent a91187f commit 645f239

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
2626
#### [23. merge k sorted lists](https://github.com/hitzzc/go-leetcode/tree/master/merge_k_sorted_lists)
2727
#### [24. swap nodes in pairs](https://github.com/hitzzc/go-leetcode/tree/master/swap_nodes_in_pairs)
2828
#### [25. reverse nodes in k group](https://github.com/hitzzc/go-leetcode/tree/master/reverse_nodes_in_k_group)
29+
#### [26. Remove Duplicates from Sorted Array](https://github.com/hitzzc/go-leetcode/tree/master/remove_duplicates_from_sorted_array)
2930

3031

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package remove_duplicates_from_sorted_array
2+
3+
func removeDuplicates(nums []int) int {
4+
if len(nums) < 2 {
5+
return len(nums)
6+
}
7+
start, preVal := 1, nums[0]
8+
for i := 1; i < len(nums); i++ {
9+
if nums[i] != preVal {
10+
start, preVal, nums[start] = start+1, nums[i], nums[i]
11+
}
12+
}
13+
return start
14+
}

0 commit comments

Comments
 (0)