package main

import "fmt"

func main() {

    data := []string{"red", "black", "orange", "white", "red", "blue", "blue"}

    fmt.Println("old data = ", data)

    afterData := sliceUnique(data)

    fmt.Println("new data = ", afterData)

}

func sliceUnique(data []string) []string {
    out := data[:1]
    for _, word := range data {
        i := 0
        for ; i < len(out); i++ {
            if word == out[i] {
                break
            }
        }
        if i == len(out) {
            out = append(out, word)
        }
    }
    return out
}

打印:

old data = [red black orange white red blue blue]
new data = [red black orange white blue]

相关文章:

  • 2021-08-30
  • 2021-07-07
  • 2021-11-20
  • 2022-12-23
  • 2021-10-29
猜你喜欢
  • 2021-11-07
  • 2022-12-23
  • 2021-09-14
  • 2022-02-06
  • 2021-06-24
  • 2021-10-13
相关资源
相似解决方案