Created
December 22, 2018 04:43
-
-
Save sharmaabhinav/fa39e58e7cc42009debf5453a3796aa2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.size = function () { | |
return collection.length | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment