0
Help us understand the problem. What are the problem?

posted at

updated at

[golang]list内の重複した値を消す

package main
import "fmt"
func main() {

	people := []string{"tanaka", "sato", "koike", "tanaka", "sato"}

	unique := make([]string, 0, len(people))

	m := make(map[string]struct{})

	for _, v := range people {
		if _, ok := m[v]; ok { //重複している際はok(true)となる
			fmt.Println(ok)
			continue //continueが終わった後再びforループの先頭に戻る
		}
		unique = append(unique, v)
		m[v] = struct{}{} //重複を消す処理

	}
	fmt.Println(unique) //出力:tanaka sato koike
}

Register as a new user and use Qiita more conveniently

  1. You can follow users and tags
  2. you can stock useful information
  3. You can make editorial suggestions for articles
What you can do with signing up
0
Help us understand the problem. What are the problem?