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条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler