英文:
How to compare 2 string slices and get elements present in 1st slice and not in 2nd slice into an another string slice in golang
问题
我有3个字符串切片:
enp_slice := []string{"10.10.10.10", "10.10.10.11"}
cachedenp_slice := []string{"10.10.10.10", "10.10.10.11", "10.10.10.12"}
result := []string{}
我想比较这两个字符串切片,并获取它们中不同的元素,并将它们存储到第三个切片中(即在cachedenp_slice中存在但在enp_slice中不存在的元素),如下所示:
result = ["10.10.10.12"]
英文:
I have 3 string slices:
enp_slice := []string{"10.10.10.10", "10.10.10.11"}
cachedenp_slice := []string{"10.10.10.10", "10.10.10.11", "10.10.10.12"}
result := []string{}
I want to compare the 2 string slices and get distict elements in both of them and store them into the 3rd slice(element present in cachedenp_slice but not in enp_slice) like following:
result = ["10.10.10.12"]
答案1
得分: 1
另一种选择是将目标切片转换为map
,然后使用这个map
检查原始切片。
示例代码
func main() {
enp_slice := []string{"10.10.10.10", "10.10.10.11"}
cachedenp_slice := []string{"10.10.10.10", "10.10.10.11", "10.10.10.12"}
fmt.Println(diffSlice(cachedenp_slice, enp_slice))
}
func diffSlice(origin, target []string) []string {
m := make(map[string]struct{}, len(target))
for _, v := range target {
m[v] = struct{}{}
}
var result []string
for _, v := range origin {
if _, ok := m[v]; !ok {
result = append(result, v)
}
}
return result
}
https://go.dev/play/p/wOM5MZnZrcd
英文:
Another option is to convert the target slice to map
, then check the origin slice with this map
Sample codes
func main() {
enp_slice := []string{"10.10.10.10", "10.10.10.11"}
cachedenp_slice := []string{"10.10.10.10", "10.10.10.11", "10.10.10.12"}
fmt.Println(diffSlice(cachedenp_slice, enp_slice))
}
func diffSlice(origin, target []string) []string {
m := make(map[string]struct{}, len(target))
for _, v := range target {
m[v] = struct{}{}
}
var result []string
for _, v := range origin {
if _, ok := m[v]; !ok {
result = append(result, v)
}
}
return result
}
答案2
得分: 0
这应该适用于你。我遍历了这两个切片,并取出第二个切片中不在第一个切片中的元素。我使用了一个名为isFound
的布尔变量来判断当前项是否被找到。以下是代码:
package main
import "fmt"
func main() {
enp_slice := []string{"10.10.10.10", "10.10.10.11"}
cachedenp_slice := []string{"10.10.10.10", "10.10.10.11", "10.10.10.12"}
result := []string{}
for _, v := range cachedenp_slice {
isFound := false
for _, w := range enp_slice {
if v == w {
isFound = true
break
}
}
if !isFound {
result = append(result, v)
}
}
fmt.Printf("%v\n", result)
}
如果对你也适用,请告诉我。
英文:
This should work for you.
I iterated over the two slices and take the elements of the second slice that are not present in the first one. I used a bool variable called isFound
to understand if the current item was found or not. Below you can find the code:
package main
import "fmt"
func main() {
enp_slice := []string{"10.10.10.10", "10.10.10.11"}
cachedenp_slice := []string{"10.10.10.10", "10.10.10.11", "10.10.10.12"}
result := []string{}
for _, v := range cachedenp_slice {
isFound := false
for _, w := range enp_slice {
if v == w {
isFound = true
break
}
}
if !isFound {
result = append(result, v)
}
}
fmt.Printf("%v\n", result)
}
Let me know if works also for you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论