LoginSignup
0
0

More than 1 year has passed since last update.

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

Last updated at Posted at 2022-05-12
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
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0