freeCodeCamp/curriculum/challenges/english/10-coding-interview-prep/data-structures/perform-a-union-on-two-sets.md

155 lines
3.0 KiB
Markdown

---
id: 587d8253367417b2b2512c6c
title: Perform a Union on Two Sets
challengeType: 1
forumTopicId: 301708
---
# --description--
In this exercise we are going to perform a union on two sets of data. We will create a method on our `Set` data structure called `union`. This method should take another `Set` as an argument and return the `union` of the two sets, excluding any duplicate values.
For example, if `setA = ['a','b','c']` and `setB = ['a','b','d','e']`, then the union of setA and setB is: `setA.union(setB) = ['a', 'b', 'c', 'd', 'e']`.
# --hints--
Your `Set` class should have a `union` method.
```js
assert(
(function () {
var test = new Set();
return typeof test.union === 'function';
})()
);
```
The union of `["a", "b", "c"]` and `["c", "d"]` should return `["a", "b", "c", "d"]`.
```js
assert(
(function () {
var setA = new Set();
var setB = new Set();
setA.add('a');
setA.add('b');
setA.add('c');
setB.add('c');
setB.add('d');
var unionSetAB = setA.union(setB);
var final = unionSetAB.values();
return (
final.indexOf('a') !== -1 &&
final.indexOf('b') !== -1 &&
final.indexOf('c') !== -1 &&
final.indexOf('d') !== -1 &&
final.length === 4
);
})()
);
```
# --seed--
## --seed-contents--
```js
class Set {
constructor() {
// This will hold the set
this.dictionary = {};
this.length = 0;
}
// This method will check for the presence of an element and return true or false
has(element) {
return this.dictionary[element] !== undefined;
}
// This method will return all the values in the set
values() {
return Object.keys(this.dictionary);
}
// This method will add an element to the set
add(element) {
if (!this.has(element)) {
this.dictionary[element] = true;
this.length++;
return true;
}
return false;
}
// This method will remove an element from a set
remove(element) {
if (this.has(element)) {
delete this.dictionary[element];
this.length--;
return true;
}
return false;
}
// This method will return the size of the set
size() {
return this.length;
}
// Only change code below this line
// Only change code above this line
}
```
# --solutions--
```js
class Set {
constructor() {
this.dictionary = {};
this.length = 0;
}
has(element) {
return this.dictionary[element] !== undefined;
}
values() {
return Object.keys(this.dictionary);
}
add(element) {
if (!this.has(element)) {
this.dictionary[element] = true;
this.length++;
return true;
}
return false;
}
remove(element) {
if (this.has(element)) {
delete this.dictionary[element];
this.length--;
return true;
}
return false;
}
size() {
return this.length;
}
union(set) {
const newSet = new Set();
this.values().forEach(value => {
newSet.add(value);
})
set.values().forEach(value => {
newSet.add(value);
})
return newSet;
}
}
```