Last active
June 15, 2018 06:34
-
-
Save sekky0905/941cc883c5d215b79d2630e303953f62 to your computer and use it in GitHub Desktop.
Goで2つのSliceから重複する値だけを抽出したSliceを生成する ref: https://qiita.com/Sekky0905/items/278a8d23fec258a6aae4
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" | |
) | |
func main() { | |
sliceA := []int{1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5} | |
sliceB := []int{6, 7, 8, 9, 5, 6, 6, 6, 6, 6, 6, 7, 8} | |
// 重複した値だけを抽出したsliceを作る | |
newSlice := ExtractDuplication(sliceA, sliceB) | |
fmt.Printf("sliceAとsliceBを重複部分を抽出 : %#v\n", newSlice) | |
} | |
// ExtractDuplication は、2つのintのスライスの中から重複した数字を抽出する | |
func ExtractDuplication(sliceA, sliceB []int) []int { | |
// 2つのsliceを結合 | |
// sliceAとsliceBの各々の重複を削除 | |
sliceA = removeDuplication(sliceA) | |
sliceB = removeDuplication(sliceB) | |
sliceC := append(sliceA, sliceB...) | |
fmt.Printf("結合したSliceC : %#v\n", sliceC) | |
sliceD := make([]int, 0) | |
m := make(map[int]struct{}, 0) | |
for _, el := range sliceC { | |
if _, ok := m[el]; ok == false { // 存在しない場合は、その値が存在することを示すためにmapに構造体を格納 | |
m[el] = struct{}{} | |
} else { // そうでない場合には、新しいSliceに追加 | |
sliceD = append(sliceD, el) | |
// 重複した数字を格納するSliceの中での重複を削除 | |
// 例 : {5, 6, 6}みたいなのは{5, 6}にする | |
sliceD = removeDuplication(sliceD) | |
} | |
} | |
return sliceD | |
} | |
// removeDuplication は、intのスライスの中から重複した数字を削除する | |
func removeDuplication(slice []int) []int { | |
m := make(map[int]struct{}) | |
newSlice := make([]int, 0) | |
for _, element := range slice { | |
// mapでは、第二引数にその値が入っているかどうかの真偽値が入っている | |
if _, ok := m[element]; ok == false { | |
m[element] = struct{}{} | |
newSlice = append(newSlice, element) | |
} | |
} | |
return newSlice | |
} |
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" | |
func main() { | |
sliceA := []int{1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5} | |
sliceB := []int{6, 7, 8, 9, 5, 6, 6, 6, 6, 6, 6} | |
// 重複した値だけを抽出したsliceを作る | |
newSlice := ExtractDuplication(sliceA, sliceB) | |
fmt.Printf("sliceAとsliceBを重複部分を抽出 : %#v\n", newSlice) | |
} | |
// ExtractDuplication は、2つのintのスライスの中から重複した数字を抽出する | |
func ExtractDuplication(sliceA, sliceB []int) []int { | |
// 2つのsliceを結合 | |
sliceC := sliceA[:] | |
sliceC = append(sliceC, sliceB...) | |
fmt.Printf("結合したSliceC : %#v\n", sliceC) | |
sliceD := make([]int, 0) | |
m := make(map[int]struct{}, 0) | |
for _, el := range sliceC { | |
if _, ok := m[el]; ok == false { // 存在しない場合は、その値が存在することを示すためにmapに構造体を格納 | |
m[el] = struct{}{} | |
} else { // そうでない場合には、新しいSliceに追加 | |
sliceD = append(sliceD, el) | |
// 重複した数字を格納するSliceの中での重複を削除 | |
// 例 : {5, 6, 6}みたいなのは{5, 6}にする | |
sliceD = removeDuplication(sliceD) | |
} | |
} | |
return sliceD | |
} | |
// removeDuplication は、intのスライスの中から重複した数字を削除する | |
func removeDuplication(slice []int) []int { | |
m := make(map[int]struct{}) | |
newSlice := make([]int, 0) | |
for _, element := range slice { | |
// mapでは、第二引数にその値が入っているかどうかの真偽値が入っている | |
if _, ok := m[element]; ok == false { | |
m[element] = struct{}{} | |
newSlice = append(newSlice, element) | |
} | |
} | |
return newSlice | |
} |
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
結合したSliceC : []int{1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 5} | |
sliceAとsliceBを重複部分を抽出 : []int{6, 5} |
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 main() { | |
sliceA := []int{1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5} | |
sliceB := []int{6, 7, 8, 9, 5, 6, 6, 6, 6, 6, 6, 7, 8} | |
// 重複した値だけを抽出したsliceを作る | |
newSlice := ExtractDuplication(sliceA, sliceB) | |
fmt.Printf("sliceAとsliceBを重複部分を抽出 : %#v\n", newSlice) | |
} | |
// ExtractDuplication は、2つのintのスライスの中から重複した数字を抽出する | |
func ExtractDuplication(sliceA, sliceB []int) []int { | |
// 2つのsliceを結合 | |
// sliceAとsliceBの各々の重複を削除 | |
sliceA = removeDuplication(sliceA) | |
sliceB = removeDuplication(sliceB) | |
sliceC := append(sliceA, sliceB...) | |
fmt.Printf("結合したSliceC : %#v\n", sliceC) | |
sliceD := make([]int, 0) | |
m := make(map[int]struct{}, 0) | |
for _, el := range sliceC { | |
if _, ok := m[el]; ok == false { // 存在しない場合は、その値が存在することを示すためにmapに構造体を格納 | |
m[el] = struct{}{} | |
} else { // そうでない場合には、新しいSliceに追加 | |
sliceD = append(sliceD, el) | |
// 重複した数字を格納するSliceの中での重複を削除 | |
// 例 : {5, 6, 6}みたいなのは{5, 6}にする | |
sliceD = removeDuplication(sliceD) | |
sort.SliceStable(sliceD, func(i, j int) bool { return sliceD[i] < sliceD[j] }) | |
} | |
} | |
return sliceD | |
} | |
// removeDuplication は、intのスライスの中から重複した数字を削除する | |
func removeDuplication(slice []int) []int { | |
m := make(map[int]struct{}) | |
newSlice := make([]int, 0) | |
for _, element := range slice { | |
// mapでは、第二引数にその値が入っているかどうかの真偽値が入っている | |
if _, ok := m[element]; ok == false { | |
m[element] = struct{}{} | |
newSlice = append(newSlice, element) | |
} | |
} | |
return newSlice | |
} |
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
結合したSliceC : []int{1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 6, 7, 8, 9, 5, 6, 6, 6, 6, 6, 6} | |
sliceAとsliceBを重複部分を抽出 : []int{6, 5} |
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 main() { | |
sliceA := []int{1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5} | |
sliceB := []int{6, 7, 8, 9, 5, 6, 6, 6, 6, 6, 6} | |
// 重複した値だけを抽出したsliceを作る | |
newSlice := ExtractDuplication(sliceA, sliceB) | |
fmt.Printf("sliceAとsliceBを重複部分を抽出 : %#v\n", newSlice) | |
} | |
// ExtractDuplication は、2つのintのスライスの中から重複した数字を抽出する | |
func ExtractDuplication(sliceA, sliceB []int) []int { | |
// 2つのsliceを結合 | |
sliceC := sliceA[:] | |
sliceC = append(sliceC, sliceB...) | |
fmt.Printf("結合したSliceC : %#v\n", sliceC) | |
sliceD := make([]int, 0) | |
m := make(map[int]struct{}, 0) | |
for _, el := range sliceC { | |
if _, ok := m[el]; ok == false { // 存在しない場合は、その値が存在することを示すためにmapに構造体を格納 | |
m[el] = struct{}{} | |
} else { // そうでない場合には、新しいSliceに追加 | |
sliceD = append(sliceD, el) | |
// 重複した数字を格納するSliceの中での重複を削除 | |
// 例 : {5, 6, 6}みたいなのは{5, 6}にする | |
sliceD = removeDuplication(sliceD) | |
sort.SliceStable(sliceD, func(i, j int) bool { return sliceD[i] < sliceD[j] }) | |
} | |
} | |
return sliceD | |
} | |
// removeDuplication は、intのスライスの中から重複した数字を削除する | |
func removeDuplication(slice []int) []int { | |
m := make(map[int]struct{}) | |
newSlice := make([]int, 0) | |
for _, element := range slice { | |
// mapでは、第二引数にその値が入っているかどうかの真偽値が入っている | |
if _, ok := m[element]; ok == false { | |
m[element] = struct{}{} | |
newSlice = append(newSlice, element) | |
} | |
} | |
return newSlice | |
} |
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
結合したSliceC : []int{1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 5} | |
sliceAとsliceBを重複部分を抽出 : []int{5, 6} |
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
結合したSliceC : []int{1, 2, 3, 4, 5, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 6, 7, 8, 9, 5, 6, 6, 6, 6, 6, 6} | |
sliceAとsliceBを重複部分を抽出 : []int{5, 6} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment