

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Object Literal Types
incomplete
2: Extra Properties
incomplete
3: Optional Object Properties
incomplete
4: Empty Object Type
incomplete
5: Discriminated Unions
incomplete
6: Sets
incomplete
7: Maps
incomplete
8: Dynamic Keys
incomplete
9: Dynamic Default Properties
incomplete
10: PropertyKey
incomplete
11: Readonly Modifier
incomplete
12: 'As Const' and Object.freeze
incomplete
13: Satisfies
incomplete
14: Function Overloads
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
TypeScript has a built-in type for sets, which are collections of unique values. You can ensure that all the values in the set are of the same type by specifying a type parameter: <T>.
// A Set that contains only strings
const justiceLeague = new Set<string>();
justiceLeague.add("Green Arrow");
justiceLeague.add("Flash");
// Error: Argument of type '2' is not assignable to parameter of type 'string'
justiceLeague.add(2);
An array can be converted into a set, which automatically removes duplicate values:
// A Set automatically removes duplicate values from an array
const names = ["plasticman", "firestorm", "plasticman"];
const justiceLeague = new Set<string>(names);
console.log(justiceLeague);
// Set { 'plasticman', 'firestorm' }
Sets also have a few other interesting methods and properties:
const justiceLeague = new Set<string>(["Atom", "Black Canary", "Blue Beetle"]);
console.log(justiceLeague.size); // 3
justiceLeague.delete("Blue Beetle");
console.log(justiceLeague.has("Blue Beetle")); // false
justiceLeague.forEach((member) => console.log(member));
// Atom
// Black Canary
Complete the findNumUniqueLabels function. It takes an array of strings and returns the number of unique values in the array.
Use a set to remove duplicates