Skip to content

Commit 9c1a0ab

Browse files
committed
Solve the "Compute the intersection of two sorted arrays" problem
1 parent 687b93f commit 9c1a0ab

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ Sorting
204204

205205
| Problem | Test | Implemented |
206206
|--------------------------------------------------------------------------|:------------:|:-----------:|
207-
| [Compute the intersection of two sorted arrays][244] | [tests][245] | |
207+
| [Compute the intersection of two sorted arrays][244] | [tests][245] | |
208208
| [Implement mergesort in-place][246] | [tests][247] | |
209209
| [Count the frequencies of characters in a sentence][248] | [tests][249] | |
210210
| [Find unique elements][250] | [tests][251] | |
@@ -647,8 +647,8 @@ Honors Class
647647
[241]: in_progress.md
648648
[242]: in_progress.md
649649
[243]: in_progress.md
650-
[244]: in_progress.md
651-
[245]: in_progress.md
650+
[244]: sorting/intersection.go
651+
[245]: sorting/intersection_test.go
652652
[246]: in_progress.md
653653
[247]: in_progress.md
654654
[248]: in_progress.md

sorting/intersection.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2015, Peter Mrekaj. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE.txt file.
4+
5+
package sorting
6+
7+
// IntersectSorted returns intersection of x and y without duplicated elements.
8+
// The time complexity is O(m+n). The O(1) additional space is needed.
9+
func IntersectSorted(x, y []int) (ixy []int) {
10+
i, j := 0, 0
11+
for i < len(x) && j < len(y) {
12+
switch ex, ey := x[i], y[j]; {
13+
case ex == ey && (i == 0 || x[i-1] != ex):
14+
ixy = append(ixy, ex)
15+
i++
16+
j++
17+
case ex <= ey:
18+
i++
19+
case ex > ey:
20+
j++
21+
}
22+
}
23+
return ixy
24+
}

sorting/intersection_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) 2015, Peter Mrekaj. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE.txt file.
4+
5+
package sorting
6+
7+
import (
8+
"math/rand"
9+
"reflect"
10+
"sort"
11+
"testing"
12+
)
13+
14+
func TestIntersectSorted(t *testing.T) {
15+
for _, test := range []struct {
16+
x, y []int
17+
want []int
18+
}{
19+
{[]int(nil), []int(nil), []int(nil)},
20+
{[]int{0}, []int(nil), []int(nil)},
21+
{[]int(nil), []int{0}, []int(nil)},
22+
{[]int{0}, []int{1}, []int(nil)},
23+
{[]int{0}, []int{0}, []int{0}},
24+
{[]int{0, 1}, []int{2}, []int(nil)},
25+
{[]int{0}, []int{0, 1}, []int{0}},
26+
{[]int{0, 1}, []int{1}, []int{1}},
27+
{[]int{0, 1}, []int{1, 2}, []int{1}},
28+
{[]int{1, 1, 1}, []int{1, 1}, []int{1}},
29+
{[]int{1, 1}, []int{1, 1, 1}, []int{1}},
30+
{[]int{1, 1, 2, 3, 4}, []int{0, 0, 1}, []int{1}},
31+
{[]int{0, 0, 1}, []int{1, 1, 2, 3, 4}, []int{1}},
32+
{[]int{2, 3, 3, 5, 5, 6, 7, 7, 8, 12}, []int{5, 5, 6, 8, 8, 9, 10, 10}, []int{5, 6, 8}},
33+
} {
34+
if got := IntersectSorted(test.x, test.y); !reflect.DeepEqual(got, test.want) {
35+
t.Errorf("IntersectSorted(%v, %v) = %v; want %v", test.x, test.y, got, test.want)
36+
}
37+
}
38+
}
39+
40+
func benchIntersectSorted(b *testing.B, size int) {
41+
b.StopTimer()
42+
x := rand.New(rand.NewSource(int64(size))).Perm(size)
43+
sort.Ints(x)
44+
y := x[size-(size/2):]
45+
b.StartTimer()
46+
for i := 0; i < b.N; i++ {
47+
IntersectSorted(x, y)
48+
}
49+
}
50+
51+
func BenchmarkIntersectSorted1e2(b *testing.B) { benchIntersectSorted(b, 1e2) }
52+
func BenchmarkIntersectSorted1e4(b *testing.B) { benchIntersectSorted(b, 1e4) }
53+
func BenchmarkIntersectSorted1e6(b *testing.B) { benchIntersectSorted(b, 1e6) }

0 commit comments

Comments
 (0)