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
}
More than 1 year has passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme