TL ; DR:
A Set holds values without repeats. Adding a value that is already in it changes nothing.
let pets = new Set<string>(["cat", "dog", "cat"]); // cat goes in once
pets.add("fish"); // a new value
pets.add("dog"); // already in it, no change
console.log(pets.size); // 3
console.log(pets.has("dog")); // true
Full lesson:
An array 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.
new Set()makes an empty set. Its type is writtenSet<string>for a set of strings.s.add(x)putsxin the sets.s.has(x)istruewhenxis in the set.s.sizegives how many values the set holds.new Set(arr)builds a set from the arrayarr. Its repeats go in once.
Checking s.has(x) is fast, even for a big set. An array's includes looks at the values one by one.
Watch the names. A set counts with size, not length. It checks with has, not includes.
A TypeScript set keeps the order you added values in. Still, it has no indexes, so s[0] doesn't work.
Your task
The camp's sign-up array, names, has some names twice. Build a set named kids from it.
Then Zoe signs up, and Ava signs up again. Add both names to kids.
Last, if kids has "Leo", 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.
