duanbi3385 2017-07-06 18:04 采纳率: 100%
浏览 2040
已采纳

如何在golang中获得两个切片的交集?

有什么有效的方法让Go的两个切片相交吗?

我想避免嵌套的类似循环的解决方案。
slice1 := []string{"foo", "bar","hello"}
slice2 := []string{"foo", "bar"}

intersection(slice1, slice2)
=> ["foo", "bar"]
字符串的顺序不重要。
  • 写回答

5条回答

  • douzi2778 2017-07-06 18:59
    关注

    Yes there are a few different ways to go about it.. Here's an example that can be optimized.

    package main
    
    import "fmt"
    
    func intersection(a []string, b []string) (inter []string) {
        // interacting on the smallest list first can potentailly be faster...but not by much, worse case is the same
        low, high := a, b
        if len(a) > len(b) {
            low = b
            high = a
        }
    
        done := false
        for i, l := range low {
            for j, h := range high {
                // get future index values
                f1 := i + 1
                f2 := j + 1
                if l == h {
                    inter = append(inter, h)
                    if f1 < len(low) && f2 < len(high) {
                        // if the future values aren't the same then that's the end of the intersection
                        if low[f1] != high[f2] {
                            done = true
                        }
                    }
                    // we don't want to interate on the entire list everytime, so remove the parts we already looped on will make it faster each pass
                    high = high[:j+copy(high[j:], high[j+1:])]
                    break
                }
            }
            // nothing in the future so we are done
            if done {
                break
            }
        }
        return
    }
    
    func main() {
        slice1 := []string{"foo", "bar", "hello", "bar"}
        slice2 := []string{"foo", "bar"}
        fmt.Printf("%+v
    ", intersection(slice1, slice2))
    }
    

    Now the intersection method defined above will only operate on slices of strings, like your example.. You can in theory create a definition that looks like this func intersection(a []interface, b []interface) (inter []interface), however you would be relying on reflection and type casting so that you can compare, which will add latency and make your code harder to read. It's probably easier to maintain and read to write a separate function for each type you care about.

    func intersectionString(a []string, b []string) (inter []string),

    func intersectionInt(a []int, b []int) (inter []int),

    func intersectionFloat64(a []Float64, b []Float64) (inter []Float64), ..ect

    You can then create your own package and reuse once you settle how you want to implement it.

    package intersection
    
    func String(a []string, b []string) (inter []string)
    
    func Int(a []int, b []int) (inter []int)
    
    func Float64(a []Float64, b []Float64) (inter []Float64)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条