Skip to content

Commit 9ce93f2

Browse files
committed
26. Remove Duplicates from Sorted Array
1 parent 31fcc46 commit 9ce93f2

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# LeetCode
22

3-
![Problems Solved](https://img.shields.io/badge/Problems%20Solved-26%20%2F%203008-1f425f?logo=leetcode)
3+
![Problems Solved](https://img.shields.io/badge/Problems%20Solved-27%20%2F%203008-1f425f?logo=leetcode)
44
![Language: Golang](https://img.shields.io/badge/language-Golang-00ADD8?logo=go)
55
![Language: Rust](https://img.shields.io/badge/language-Rust-00ADD8?logo=rust)
66
![Language: Bash](https://img.shields.io/badge/language-Bash-00ADD8?logo=gnubash&logoColor=f5f5f5)
@@ -17,6 +17,7 @@
1717
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [Go](go/0013_roman_to_integer) | 🟢 | Hash Table, Math, String |
1818
| 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [Go](go/0017_letter_combinations_phone) | 🟡 | Hash Table, String, Backtracking |
1919
| 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Go](go/0020_valid_parentheses) | 🟢 | String, Stack |
20+
| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [Go](go/0026_remove_duplicates_from_array) | 🟢 | Array, Pointer |
2021
| 35 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [Go](go/0035_search_insert_position), [Rust](rust/_0035_search_insert_position) | 🟢 | Array, Binary Search |
2122
| 50 | [Pow(x, n)](https://leetcode.com/problems/powx-n/) | [Go](go/0050_powx_n) | 🟡 | Math, Recursion |
2223
| 69 | [Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [Go](go/0069_sqrtx), [Rust](rust/_0069_sqrtx) | 🟢 | Math, Binary Search |
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package remove_duplicates_from_array
2+
3+
func removeDuplicates(nums []int) int {
4+
if len(nums) <= 1 {
5+
return len(nums)
6+
}
7+
8+
lastNum := nums[0] // Last number for comparing
9+
rCur, wCur := 1, 1 // Read and write cursors
10+
11+
for rCur < len(nums) {
12+
if nums[rCur] > lastNum {
13+
lastNum = nums[rCur]
14+
nums[wCur] = lastNum
15+
wCur++
16+
}
17+
18+
rCur++
19+
}
20+
21+
return wCur
22+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package remove_duplicates_from_array
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_containsDuplicate(t *testing.T) {
9+
tests := []struct {
10+
input []int
11+
want int
12+
wantArr []int
13+
}{
14+
{input: []int{0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 4}, want: 5, wantArr: []int{0, 1, 2, 3, 4, 0, 0, 0, 0, 0}},
15+
{input: []int{1, 1, 2}, want: 2, wantArr: []int{1, 2, 0}},
16+
{input: []int{1, 2}, want: 2, wantArr: []int{1, 2}},
17+
{input: []int{1}, want: 1, wantArr: []int{1}},
18+
{input: []int{}, want: 0, wantArr: []int{}},
19+
}
20+
21+
for i, tc := range tests {
22+
got := removeDuplicates(tc.input)
23+
24+
if tc.want != got {
25+
t.Fatalf("Case #%d: expected: %v, got: %v", i+1, tc.want, got)
26+
}
27+
28+
if !reflect.DeepEqual(tc.wantArr[:got], tc.input[:got]) {
29+
t.Fatalf("Case #%d: invalid modified slice - expected: %v, got: %v", i+1, tc.wantArr[:got], tc.input[:got])
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)