# base/collection/set (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/base/collection/set/

Set (a collection without duplicates).

## Set extends Object

Set (a collection without duplicates).

Example: Union and intersection

```js
import { Set } from "./build/vanilla.js";

const left = new Set();
left.add(1);
left.add(2);
left.add(2);
const right = new Set();
right.add(2);
right.add(3);
console.log(left.getCount(), left.union(right).toArray(), left.intersection(right).toArray(), left.difference(right).toArray());
```

- `constructor()` (constructor, since 0.0.8): Constructor.
- `add(item)` (method, since 0.0.8): Adds data.
  - `item` T: Item to add. (unchanged if already present)
- `remove(item)` (method, since 0.0.8): Removes data.
  - `item` T: Item to remove.
  - returns boolean: True if it existed and was removed.
- `contains(item)` (method, since 0.0.8): Returns whether the element is contained.
  - `item` T: Item to look for.
  - returns boolean: True if present.
- `union(otherSet)` (method, since 0.0.8): Union. (Union)
  - `otherSet` Set<T>: Other set to combine.
  - returns Set<T>: New set with all items of both sets.
- `intersection(otherSet)` (method, since 0.0.8): Intersection. (Intersection)
  - `otherSet` Set<T>: Other set to compare.
  - returns Set<T>: New set with only the items present in both sets.
- `difference(otherSet)` (method, since 0.0.8): Difference. (Difference)
  - `otherSet` Set<T>: Other set to subtract.
  - returns Set<T>: New set with the items in this set that are not in the other set.
- `symmetricDifference(otherSet)` (method, since 0.0.8): Symmetric difference (complement). (Symmetric Difference)
  - `otherSet` Set<T>: Other set to compare.
  - returns Set<T>: New set with the items present in only one of the two sets.
- `clear()` (method, since 0.0.8): Removes all.
- `getCount()` (method, since 0.0.8): Returns the element count.
  - returns number: Number of items.
- `toArray()` (method, since 0.0.8): Returns an array of all elements.
  - returns T[]: New array of items in insertion order.
