2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 8d1323c8c441eddfaeb5bdef
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Create a Set Class
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								challengeType: 1
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 301632
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Description
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'description' >  
						 
					
						
							
								
									
										
										
										
											2019-07-18 17:32:12 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In this exercise we are going to create a class named < code > Set< / code >  to emulate an abstract data structure called "set". A set is like an array, but it cannot contain duplicate values. The typical use for a set is to simply check for the presence of an item.
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 14:17:07 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								We can see how ES6 set object works in the example below-
							 
						 
					
						
							
								
									
										
										
										
											2019-06-04 00:07:22 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const set1 = new Set([1, 2, 3, 5, 5, 2, 0]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(set1);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// output: {1, 2, 3, 5, 0}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(set1.has(1));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// output: true
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								console.log(set1.has(6));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// output: false
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 14:17:07 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								First, we will create an add method that adds a value to our set collection as long as the value does not already exist in the set.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Then we will create a remove method that removes a value from the set collection if it already exists.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								And finally, we will create a size method that returns the number of elements inside the set collection.
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Instructions
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'instructions' >  
						 
					
						
							
								
									
										
										
										
											2019-07-18 17:32:12 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 14:17:07 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Create an < code > add< / code >  method that adds a unique value to the set collection and returns < code > true< / code >  if the value was successfully added and < code > false< / code >  otherwise.
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 14:17:07 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Create a < code > remove< / code >  method that accepts a value and checks if it exists in the set. If it does, then this method should remove it from the set collection, and return < code > true< / code > . Otherwise, it should return < code > false< / code > .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Create a < code > size< / code >  method that returns the size of the set collection.
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 14:17:07 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								## Tests
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'tests' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```yml
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								tests:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  text: Your < code > Set</ code >  class should have an < code > add</ code >  method.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 23:56:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert((function(){var test = new Set(); return (typeof test.add === 'function')}()));
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: Your < code > add</ code >  method should not add duplicate values.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 23:56:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert((function(){var test = new Set(); test.add('a'); test.add('b'); test.add('a'); var vals = test.values(); return (vals[0] === 'a' & &  vals[1] === 'b' & &  vals.length === 2)}()));
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: Your < code > add</ code >  method should return < code > true</ code >  when a value has been successfully added.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 23:56:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert((function(){var test = new Set(); var result = test.add('a'); return (result != undefined) & &  (result === true);}()));
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: Your < code > add</ code >  method should return < code > false</ code >  when a duplicate value is added.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 23:56:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert((function(){var test = new Set(); test.add('a'); var result = test.add('a'); return (result != undefined) & &  (result === false);}()));
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 14:17:07 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: Your < code > Set</ code >  class should have a < code > remove</ code >  method.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 23:56:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert((function(){var test = new Set(); return (typeof test.remove === 'function')}()));
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 14:17:07 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: Your < code > remove</ code >  method should only remove items that are present in the set.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 23:56:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert.deepEqual((function(){var test = new Set(); test.add('a');test.add('b');test.remove('c'); return test.values(); })(), ['a', 'b']);
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 14:17:07 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: Your < code > remove</ code >  method should remove the given item from the set.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 23:56:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert((function(){var test = new Set(); test.add('a');test.add('b');test.remove('a'); var vals = test.values(); return (vals[0] === 'b' & &  vals.length === 1)}()));
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 14:17:07 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: Your < code > Set</ code >  class should have a < code > size</ code >  method.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 23:56:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert((function(){var test = new Set(); return (typeof test.size === 'function')}()));
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 14:17:07 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  text: The < code > size</ code >  method should return the number of elements in the collection.
							 
						 
					
						
							
								
									
										
										
										
											2019-07-24 23:56:32 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert((function(){var test = new Set(); test.add('a');test.add('b');test.remove('a');return (test.size() === 1)}()));
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Challenge Seed
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'challengeSeed' >  
						 
					
						
							
								
									
										
										
										
											2019-07-18 17:32:12 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< div  id = 'js-seed' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 14:17:07 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								class Set {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    constructor() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // collection will hold our set
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.collection = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    // this method will check for the presence of an element and return true or false
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 14:17:07 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    has(element) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return this.collection.indexOf(element) !== -1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    // this method will return all the values in the set
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 14:17:07 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    values() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return this.collection;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    // change code below this line
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 14:17:07 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // write your add method here
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // write your remove method here
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // write your size method here
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    // change code above this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Solution
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'solution' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 14:17:07 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								class Set {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  constructor() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.collection = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  has(element) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return this.collection.indexOf(element) !== -1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  values() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return this.collection;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  add(element) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (!this.has(element)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      this.collection.push(element);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  remove(element) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (this.has(element)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      let i = this.collection.indexOf(element);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      this.collection.splice(i, 1);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return true;
							 
						 
					
						
							
								
									
										
										
										
											2018-11-25 23:26:33 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2019-01-19 14:17:07 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  size() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return this.collection.length;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2018-11-25 23:26:33 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2019-07-18 17:32:12 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								< / section >