2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
title: Create and Add to Sets in ES6
|
|
|
|
---
|
2019-07-24 00:59:27 -07:00
|
|
|
# Create and Add to Sets in ES6
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Problem Explanation
|
2019-03-21 11:03:36 -05:00
|
|
|
|
|
|
|
To solve this problem, you have to add an array of items to the set.
|
|
|
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Hints
|
|
|
|
|
|
|
|
### Hint 1
|
2019-03-21 11:03:36 -05:00
|
|
|
|
|
|
|
Use the add function to add an array of strings to the set.
|
|
|
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
### Hint 2
|
2019-03-21 11:03:36 -05:00
|
|
|
|
|
|
|
Use the length attribute on the values of the Set.
|
|
|
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
---
|
|
|
|
## Solutions
|
2019-03-21 11:03:36 -05:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
2019-03-21 11:03:36 -05:00
|
|
|
|
|
|
|
```js
|
|
|
|
function checkSet() {
|
|
|
|
var set = new Set([1, 2, 3, 3, 2, 1, 2, 3, 1]);
|
|
|
|
set.add("Taco");
|
|
|
|
set.add("Cat");
|
|
|
|
set.add("Awesome");
|
|
|
|
console.log(Array.from(set));
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkSet();
|
|
|
|
```
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
#### Code Explanation
|
2019-03-21 11:03:36 -05:00
|
|
|
|
|
|
|
* Creating a set object as shown in pre-written code will create the set without duplicate objects.
|
|
|
|
* Therefore, by using the add function, we can add items to the set and they will not be duplicated, and will still be represented in the array.
|
|
|
|
|
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
</details>
|