比较两个切片并删除Golang中的唯一值

时间:2016-08-01 14:31:38

标签: arrays go

我有这个,但它无法正常工作:

var i int
var names = []string{"F5", "F7", "C6", "H5", "G5"}
var board = []string{"B4", "D4", "G5", "F5", "F7", "C6"}

for i = 0; i < len(names)-1; i++ {
    for k := 0; k < len(board)-1; k++ {
        if names[i] != board[k] {
            names = append(names[:i], names[i+1:]...)
        }
    }
}
fmt.Printf("%s ", names[i])

3 个答案:

答案 0 :(得分:2)

您正在内部names循环中更新for。但是,您的内部循环在逻辑上也是不正确的。 board中的第一个值不等于names中的值将清空names。这也会导致index out of range错误。

您可以尝试这样的事情:

var names = []string{"F5", "F7", "C6", "H5", "G5"}
var board = []string{"B4", "D4", "G5", "F5", "F7", "C6"}

results := make([]string, 0) // slice tostore the result

for i := 0; i < len(names); i++ {
    for k := 0; k < len(board); k++ {
        if names[i] != board[k] {               
            continue
        }
        // append a value in result only if
        // it exists both in names and board
        results = append(results, names[i])
    }
}
fmt.Printf("%v %d \n", results, len(results))

示例:https://play.golang.org/p/cQpzbvCGpO

答案 1 :(得分:1)

您可以从其中一个阵列中创建map并避免嵌套循环,例如:

package main

import (
    "fmt"
)

func main() {

    var names = []string{"F5", "F7", "C6", "H5", "G5"}
    var board = []string{"B4", "D4", "G5", "F5", "F7", "C6"}

    var board_map = make(map[string]bool)

    for _, ele := range board {
        board_map[ele] = true
    }

    var result []string

    for _, name := range names {
        if board_map[name] {
            result = append(result, name)
        }
    }

    fmt.Println(result)

}

Go Playground

答案 2 :(得分:0)

您需要两个切片的交集(删除第一个切片中的唯一值),尝试此工作示例代码(并注意i++):

package main

import "fmt"

func main() {
    names := []string{"F5", "F7", "C6", "H5", "G5"}
    board := []string{"B4", "D4", "G5", "F5", "F7", "C6"}
    for i := 0; i < len(names); {
        exist := false
        for _, b := range board {
            if b == names[i] {
                exist = true
                break
            }
        }
        if !exist {
            names = append(names[:i], names[i+1:]...) // delete names[i]
        } else {
            i++
        }
    }
    fmt.Println(names) // [F5 F7 C6 G5]
}

输出:

[F5 F7 C6 G5]

你也可以将它用于两个切片与新result的交集,试试这个工作示例代码:

package main

import "fmt"

func main() {
    names := []string{"F5", "F7", "C6", "H5", "G5"}
    board := []string{"B4", "D4", "G5", "F5", "F7", "C6"}
    result := make([]string, 0, 11)
    for _, v := range names {
        exist := false
        for _, w := range board {
            if v == w {
                exist = true
                break
            }
        }
        if exist {
            result = append(result, v)
        }
    }
    fmt.Println(result) // [F5 F7 C6 G5]
}

输出:

[F5 F7 C6 G5]

或者如果您需要两个切片的并集,请尝试以下工作示例代码:

package main

import "fmt"

func main() {
    names := []string{"F5", "F7", "C6", "H5", "G5"}
    board := []string{"B4", "D4", "G5", "F5", "F7", "C6"}
    for _, b := range board {
        exist := false
        for _, n := range names {
            if n == b {
                exist = true
                break
            }
        }
        if !exist {
            names = append(names, b)
        }
    }
    fmt.Println(names) // [F5 F7 C6 H5 G5 B4 D4]
}

输出:

[F5 F7 C6 H5 G5 B4 D4]