TL ; DR:
Go has no set type. A map with bool values does the job. Each value is a key, set to true.
pets := map[string]bool{} // an empty set
pets["cat"] = true
pets["dog"] = true
pets["cat"] = true // already in it, no change
fmt.Println(len(pets)) // 2
fmt.Println(pets["dog"]) // true
fmt.Println(pets["fish"]) // false
Full lesson:
A slice keeps every value you put in it, repeats too. Sometimes you only need to know which values there are. A set is made for that.
A set holds each value once. Adding a value that is already in it changes nothing.
Go has no set type. A map with bool values does the job. A key is in a map only once, just as a set needs.
map[string]bool{}makes an empty set of strings.s[x] = trueputsxin the sets.s[x]istruewhenxis in the set.len(s)gives how many values the set holds.
Reading a key that isn't there gives false, the zero value of bool. So if s[x] is enough. The comma-ok idiom from Maps & the ok Idiom works too.
Checking s[x] is fast, even for a big set. In a slice, you'd look at the values one by one.
Go can't build the map straight from a slice. A for loop puts the values in, one at a time. A repeat sets the same key again, so it is stored once.
A map has no order. Its keys may come out in any order.
Your task
The camp's sign-up slice, names, has some names twice. Build a set named kids from it: an empty map[string]bool, filled by a loop.
Then Zoe signs up, and Ava signs up again. Add both names to kids.
Last, if "Leo" is in kids, print Leo is coming.
The code already prints the number of kids twice.
Expected output:
3 kids
4 kids
Leo is comingFree plan: 30 tutor messages. PRO is unlimited.
