We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Sets

JavaScript added support for sets (still waiting on you to do the same, Golang). A set is just a collection of unique values. Sets are fantastic for de-duplication and checking if a value exists in a collection.

const set = new Set([1, 2, 3, 4, 5, 5, 5, 5]);
console.log(set);
// Set { 1, 2, 3, 4, 5 }

Notice that all duplicate instances of 5 were removed from the set. You can also add and delete values dynamically:

const set = new Set();
set.add("bertholdt");
set.add("reiner");
set.add("annie");
set.add("bertholdt");
console.log(set);
// Set { 'bertholdt', 'reiner', 'annie' }

set.delete("annie");
console.log(set);
// Set { 'bertholdt', 'reiner' }

Assignment

Network conditions and retries are causing Textio to deliver duplicate email messages to our users. Complete the deduplicateEmails function. It takes an array of strings emails and returns a new array containing only unique messages.

Tip

The ... spread operator also works for sets!