【问题标题】:how to get value from slice A which is not in Slice B? [duplicate]如何从不在切片 B 中的切片 A 中获取值? [复制]
【发布时间】:2021-11-01 04:37:53
【问题描述】:

我有两个切片 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 {
        for _, b := range B {
            if a == b {
                fmt.Printf("%s == %s\n", a,b)
                temp = append(temp, a)
                break
            }       
        }
        
    }
    
}


【问题讨论】:

    标签: go


    【解决方案1】:

    你几乎明白了。请注意是否在 B 中找到了 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)
    }
    

    【讨论】:

      【解决方案2】:
      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】:
        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)
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-24
          • 2017-05-13
          • 2021-01-12
          • 1970-01-01
          • 2018-09-30
          • 1970-01-01
          相关资源
          最近更新 更多