Last active
August 29, 2015 14:08
-
-
Save tma15/1277c7826a67a1c76212 to your computer and use it in GitHub Desktop.
Count overlapping elements between two sets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sort" | |
) | |
func CountDuplicateElem(x, y []string) int { | |
// fmt.Println(x) | |
// fmt.Println(y) | |
i := 0 | |
j := 0 | |
num_match := 0 | |
num_cmp := 0 | |
for { | |
if i >= len(x){ | |
break | |
} | |
for { | |
num_cmp += 1 | |
// fmt.Println("i", i, "j", j) | |
// fmt.Println("xi", x[i], "yj", y[j]) | |
if j >= len(y) { // 位置jがyの長さを超えたら終了 | |
break | |
} | |
if x[i] < y[j] { // 辞書順でx[i]がy[j]よりも小さければ終了 | |
break // ソートされていればjより大きな位置の文字で一致することは無い | |
} | |
if x[i] == y[j] { | |
num_match += 1 | |
j += 1 | |
break | |
} | |
j += 1 | |
} | |
// fmt.Println("match", num_match) | |
i += 1 | |
} | |
// fmt.Println(num_match) | |
// fmt.Println("CMP", num_cmp) | |
return num_match | |
} | |
func main() { | |
a := []string{"a", "c", "b"} | |
b := []string{"b", "d", "e"} | |
sort.Strings(a) | |
sort.Strings(b) | |
m1 := CountDuplicateElem(a, b) | |
fmt.Println(m1) | |
c := []string{"a", "c", "b", "e"} | |
d := []string{"b", "d", "e"} | |
sort.Strings(c) | |
sort.Strings(d) | |
m2 := CountDuplicateElem(c, d) | |
fmt.Println(m2) | |
e := []string{"a", "c", "b", "e", "f", "g"} | |
f := []string{"b", "d", "e", "x", "g"} | |
sort.Strings(e) | |
sort.Strings(f) | |
m3 := CountDuplicateElem(e, f) | |
fmt.Println(m3) | |
// fmt.Println("a" > "b") | |
// fmt.Println("b" > "a") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment