```js
-function Set() {
- // the var collection will hold the set
- var collection = [];
- // this method will check for the presence of an element and return true or false
- this.has = function(element) {
- return (collection.indexOf(element) !== -1);
- };
- // this method will return all the values in the set
- this.values = function() {
- return collection;
- };
- // this method will add an element to the set
- this.add = function(element) {
- if(!this.has(element)){
- collection.push(element);
- return true;
- }
- return false;
- };
- // this method will remove an element from a set
- this.remove = function(element) {
- if(this.has(element)){
- var index = collection.indexOf(element);
- collection.splice(index,1);
- return true;
- }
- return false;
- };
- // this method will return the size of the collection
- this.size = function() {
- return collection.length;
- };
- // this method will return the union of two sets
- this.union = function(otherSet) {
- var unionSet = new Set();
- var firstSet = this.values();
- var secondSet = otherSet.values();
- firstSet.forEach(function(e){
- unionSet.add(e);
- });
- secondSet.forEach(function(e){
- unionSet.add(e);
- });
- return unionSet;
- };
- // this method will return the intersection of two sets as a new set
- this.intersection = function(otherSet) {
- var intersectionSet = new Set();
- var firstSet = this.values();
- firstSet.forEach(function(e){
- if(otherSet.has(e)){
- intersectionSet.add(e);
- }
- });
- return intersectionSet;
- };
- // this method will return the difference of two sets as a new set
- this.difference = function(otherSet) {
- var differenceSet = new Set();
- var firstSet = this.values();
- firstSet.forEach(function(e){
- if(!otherSet.has(e)){
- differenceSet.add(e);
- }
- });
- return differenceSet;
- };
- // change code below this line
- // change code above this line
+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;
+ }
+ // This is our union method from that lesson
+ union(set) {
+ const newSet = new Set();
+ this.values().forEach(value => {
+ newSet.add(value);
+ })
+ set.values().forEach(value => {
+ newSet.add(value);
+ })
+
+ return newSet;
+ }
+ // This is our intersection method from that lesson
+ intersection(set) {
+ const newSet = new Set();
+
+ let largeSet;
+ let smallSet;
+ if (this.dictionary.length > set.length) {
+ largeSet = this;
+ smallSet = set;
+ } else {
+ largeSet = set;
+ smallSet = this;
+ }
+
+ smallSet.values().forEach(value => {
+ if (largeSet.dictionary[value]) {
+ newSet.add(value);
+ }
+ })
+
+ return newSet;
+ }
+ // This is our difference method from that lesson
+ difference(set) {
+ const newSet = new Set();
+
+ this.values().forEach(value => {
+ if (!set.dictionary[value]) {
+ newSet.add(value);
+ }
+ })
+
+ return newSet;
+ }
+ // change code below this line
+ // change code above this line
}
```
@@ -122,7 +139,97 @@ function Set() {
```js
-function Set() {var collection = []; this.has = function(e){return(collection.indexOf(e) !== -1);};this.values = function() {return collection;};this.add = function(element) {if (!this.has(element)) {collection.push(element);return true;} else {return false;}};this.remove = function(element) {if(this.has(element)) {var i = collection.indexOf(element);collection.splice(i, 1);return true;}return false;};this.size = function() {return collection.length;};this.union = function(set) {var u = new Set();var c = this.values();var s = set.values();c.forEach(function(element){u.add(element);});s.forEach(function(element){u.add(element);});return u;};this.intersection = function(set) {var i = new Set();var c = this.values();c.forEach(function(element){if(s.has(element)) i.add(element);});};this.difference = function(set) {var d = new Set();var c = this.values();c.forEach(function(e){if(!set.has(e)) d.add(e);});};this.subset = function(set) {var isSubset = true;var c = this.values();c.forEach(function(e){if(!set.has(e)) isSubset = false;});return isSubset;};}
+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;
+ }
+
+ intersection(set) {
+ const newSet = new Set();
+
+ let largeSet;
+ let smallSet;
+ if (this.dictionary.length > set.length) {
+ largeSet = this;
+ smallSet = set;
+ } else {
+ largeSet = set;
+ smallSet = this;
+ }
+
+ smallSet.values().forEach(value => {
+ if (largeSet.dictionary[value]) {
+ newSet.add(value);
+ }
+ })
+
+ return newSet;
+ }
+
+ difference(set) {
+ const newSet = new Set();
+
+ this.values().forEach(value => {
+ if (!set.dictionary[value]) {
+ newSet.add(value);
+ }
+ })
+
+ return newSet;
+ }
+
+ subset(set) {
+ for(const value of this.values()){
+ if(!set.dictionary[value]) return false;
+ }
+ return true
+ }
+}
```
diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-union-on-two-sets.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-union-on-two-sets.english.md
index 273b0fdb17..f5c7f3e0c3 100644
--- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-union-on-two-sets.english.md
+++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-a-union-on-two-sets.english.md
@@ -34,41 +34,47 @@ tests:
```js
-function Set() {
- // the var collection will hold the set
- var collection = [];
- // this method will check for the presence of an element and return true or false
- this.has = function(element) {
- return (collection.indexOf(element) !== -1);
- };
- // this method will return all the values in the set
- this.values = function() {
- return collection;
- };
- // this method will add an element to the set
- this.add = function(element) {
- if(!this.has(element)){
- collection.push(element);
- return true;
- }
- return false;
- };
- // this method will remove an element from a set
- this.remove = function(element) {
- if(this.has(element)){
- var index = collection.indexOf(element);
- collection.splice(index,1);
- return true;
- }
- return false;
- };
- // this method will return the size of the set
- this.size = function() {
- return collection.length;
- };
- // change code below this line
+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;
+ }
- // change code above this line
+ 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;
+ }
+ // change code below this line
+
+ // change code above this line
}
```
@@ -79,45 +85,55 @@ function Set() {
```js
-function Set() {
- var collection = [];
+class Set {
+ constructor() {
+ this.dictionary = {};
+ this.length = 0;
+ }
- this.has = function(element) {
- return (collection.indexOf(element) !== -1);
- };
+ has(element) {
+ return this.dictionary[element] !== undefined;
+ }
- this.values = function() {
- return collection;
- };
+ values() {
+ return Object.keys(this.dictionary);
+ }
- this.add = function(element) {
- if(!this.has(element)){
- collection.push(element);
- return true;
- }
- return false;
- };
+ add(element) {
+ if (!this.has(element)) {
+ this.dictionary[element] = true;
+ this.length++;
+ return true;
+ }
- this.remove = function(element) {
- if(this.has(element)){
- var index = collection.indexOf(element);
- collection.splice(index,1);
- return true;
- }
- return false;
- };
+ return false;
+ }
- this.size = function() {
- return collection.length;
- };
+ remove(element) {
+ if (this.has(element)) {
+ delete this.dictionary[element];
+ this.length--;
+ return true;
+ }
- this.union = function(anotherSet){
- const newSet = new Set();
- const addToSet = el => newSet.add(el);
- this.values().forEach(addToSet);
- anotherSet.values().forEach(addToSet);
- return newSet;
- };
+ 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;
+ }
}
```
diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-an-intersection-on-two-sets-of-data.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-an-intersection-on-two-sets-of-data.english.md
index 5c06e4ff79..1e41fc3245 100644
--- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-an-intersection-on-two-sets-of-data.english.md
+++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/perform-an-intersection-on-two-sets-of-data.english.md
@@ -35,53 +35,58 @@ tests:
```js
-function Set() {
- // the var collection will hold the set
- var collection = [];
- // this method will check for the presence of an element and return true or false
- this.has = function(element) {
- return (collection.indexOf(element) !== -1);
- };
- // this method will return all the values in the set
- this.values = function() {
- return collection;
- };
- // this method will add an element to the set
- this.add = function(element) {
- if(!this.has(element)){
- collection.push(element);
- return true;
- }
- return false;
- };
- // this method will remove an element from a set
- this.remove = function(element) {
- if(this.has(element)){
- var index = collection.indexOf(element);
- collection.splice(index,1);
- return true;
- }
- return false;
- };
- // this method will return the size of the collection
- this.size = function() {
- return collection.length;
- };
- // this method will return the union of two sets
- this.union = function(otherSet) {
- var unionSet = new Set();
- var firstSet = this.values();
- var secondSet = otherSet.values();
- firstSet.forEach(function(e){
- unionSet.add(e);
- });
- secondSet.forEach(function(e){
- unionSet.add(e);
- });
- return unionSet;
- };
- // change code below this line
- // change code above this line
+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;
+ }
+ // This is our union method from that lesson
+ union(set) {
+ const newSet = new Set();
+ this.values().forEach(value => {
+ newSet.add(value);
+ })
+ set.values().forEach(value => {
+ newSet.add(value);
+ })
+
+ return newSet;
+ }
+ // change code below this line
+ // change code above this line
}
```
@@ -93,61 +98,77 @@ function Set() {
```js
-function Set() {
- // the var collection will hold the set
- var collection = [];
- // this method will check for the presence of an element and return true or false
- this.has = function(element) {
- return (collection.indexOf(element) !== -1);
- };
- // this method will return all the values in the set
- this.values = function() {
- return collection;
- };
- // this method will add an element to the set
- this.add = function(element) {
- if(!this.has(element)){
- collection.push(element);
- return true;
- }
- return false;
- };
- // this method will remove an element from a set
- this.remove = function(element) {
- if(this.has(element)){
- var index = collection.indexOf(element);
- collection.splice(index,1);
- return true;
- }
- return false;
- };
- // this method will return the size of the collection
- this.size = function() {
- return collection.length;
- };
- // this method will return the union of two sets
- this.union = function(otherSet) {
- var unionSet = new Set();
- var firstSet = this.values();
- var secondSet = otherSet.values();
- firstSet.forEach(function(e){
- unionSet.add(e);
- });
- secondSet.forEach(function(e){
- unionSet.add(e);
- });
- return unionSet;
- };
- this.intersection = function(otherSet) {
- var intersectionSet = new Set();
- var firstSet = this.values();
- firstSet.forEach(function(e) {
- if (otherSet.has(e)) {
- intersectionSet.add(e);
- }
- })
- return intersectionSet;
+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;
+ }
+
+ intersection(set) {
+ const newSet = new Set();
+
+ let largeSet;
+ let smallSet;
+ if (this.dictionary.length > set.length) {
+ largeSet = this;
+ smallSet = set;
+ } else {
+ largeSet = set;
+ smallSet = this;
+ }
+
+ smallSet.values().forEach(value => {
+ if (largeSet.dictionary[value]) {
+ newSet.add(value);
+ }
+ })
+
+ return newSet;
+ }
}
```