Skip to content

Instantly share code, notes, and snippets.

@sharmaabhinav
Created December 22, 2018 04:43
Show Gist options
  • Save sharmaabhinav/fa39e58e7cc42009debf5453a3796aa2 to your computer and use it in GitHub Desktop.
Save sharmaabhinav/fa39e58e7cc42009debf5453a3796aa2 to your computer and use it in GitHub Desktop.
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