英文:
how to get value from slice A which is not in Slice B?
问题
我有两个切片 A := []string{"a","b","c","d"}
和 B := []string{"a","b"}
。
如何从切片 A 中获取 ["c","d"]
?
我尝试了各种方法,但仍然没有得到我想要的结果。谢谢。
package main
import (
"fmt"
)
func main() {
A := []string{"a","b","c","d"}
B := []string{"a","b"}
temp := []string{}
for _, a := range A {
found := false
for _, b := range B {
if a == b {
found = true
break
}
}
if !found {
temp = append(temp, a)
}
}
fmt.Println(temp)
}
以上是一个可以实现你想要的功能的示例代码。它使用了两个循环来比较切片 A 和切片 B 中的元素,如果元素在切片 B 中不存在,则将其添加到临时切片 temp 中。最后,打印出 temp 切片的内容,即 ["c","d"]
。希望对你有帮助!
英文:
I have two slice A := []string{"a","b","c","d"}
and B := []string{"a","b"}
.
How to get ["c","d"]
from slice A?
I've tried various ways but still not getting the result I want. Thank you
package main
import (
"fmt"
)
func main() {
A := []string{"a","b","c","d"}
B := []string{"a","b"}
temp := []string{}
for _, a := range A {
for _, b := range B {
if a == b {
fmt.Printf("%s == %s\n", a,b)
temp = append(temp, a)
break
}
}
}
}
答案1
得分: 1
你差不多理解了。请注意,如果在B中找到了a,则说明a已经存在。如果在B中没有找到a,则将a添加到结果中。
func main() {
A := []string{"a", "b", "c", "d"}
B := []string{"a", "b"}
var result []string
for _, a := range A {
found := false
for _, b := range B {
if a == b {
found = true
break
}
}
if !found {
result = append(result, a)
}
}
fmt.Println(result)
}
英文:
You almost got it. Note if a
was found in B
. If a
was not found in B
, then add a
to the result.
func main() {
A := []string{"a", "b", "c", "d"}
B := []string{"a", "b"}
var result []string
for _, a := range A {
found := false
for _, b := range B {
if a == b {
found = true
break
}
}
if !found {
result = append(result, a)
}
}
fmt.Println(result)
}
答案2
得分: 0
func main() {
A := []string{"a", "b", "c", "d"}
B := []string{"a", "b"}
var outterLoop, innerLoop []string
if len(A) > len(B) {
outterLoop = A
innerLoop = B
} else {
outterLoop = B
innerLoop = A
}
temp := []string{}
for _, b := range outterLoop {
found := false
for _, a := range innerLoop {
if a == b {
found = true
}
}
if !found {
temp = append(temp, b)
}
}
for _, t := range temp {
println(t)
}
}
以上是要翻译的代码部分。
英文:
func main() {
A := []string{"a", "b", "c", "d"}
B := []string{"a", "b"}
var outterLoop, innerLoop []string
if len(A) > len(B) {
outterLoop = A
innerLoop = B
} else {
outterLoop = B
innerLoop = A
}
temp := []string{}
for _, b := range outterLoop {
found := false
for _, a := range innerLoop {
if a == b {
found = true
}
}
if !found {
temp = append(temp, b)
}
}
for _, t := range temp {
println(t)
}
}
答案3
得分: 0
package main
import "fmt"
func find(a, b []string) []string {
bm := make(map[string]struct{}, len(b))
ok := false
for i := range b {
if _, ok = bm[b[i]]; !ok {
bm[b[i]] = struct{}{}
}
}
res := make([]string, 0)
for i := range a {
if _, ok = bm[a[i]]; !ok {
res = append(res, a[i])
}
}
return res
}
func main() {
a := []string{"a", "b", "c", "d"}
b := []string{"a", "b"}
t := find(a, b)
fmt.Println(t)
}
package main
import "fmt"
func find(a, b []string) []string {
bm := make(map[string]struct{}, len(b))
ok := false
for i := range b {
if _, ok = bm[b[i]]; !ok {
bm[b[i]] = struct{}{}
}
}
res := make([]string, 0)
for i := range a {
if _, ok = bm[a[i]]; !ok {
res = append(res, a[i])
}
}
return res
}
func main() {
a := []string{"a", "b", "c", "d"}
b := []string{"a", "b"}
t := find(a, b)
fmt.Println(t)
}
英文:
package main
import "fmt"
func find(a, b []string) []string {
bm := make(map[string]struct{}, len(b))
ok := false
for i := range b {
if _, ok = bm[b[i]]; !ok {
bm[b[i]] = struct{}{}
}
}
res := make([]string, 0)
for i := range a {
if _, ok = bm[a[i]]; !ok {
res = append(res, a[i])
}
}
return res
}
func main() {
a := []string{"a", "b", "c", "d"}
b := []string{"a", "b"}
t := find(a, b)
fmt.Println(t)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论