# The Set data structure

## #data-structures series

Posted on February 13, 2016

The #data-structures series is a collection of posts about reimplemented data structures in JavaScript.

If you are not familiar with data structures, a quick introduction and the full list of reimplemented data structures can be found in the introduction post of the series on data structures in JavaScript.

If you feel comfortable with the concept of each data structure and only want to see the code, have a look at the summary post of the series. It removes all explanations and contains only the JavaScript code for all data structures discussed in the series.

## Get the code on Github

Of course, all the code can also be found on Github in the repository data-structures-in-javascript.

# The Set data structure

## Definition

A Set is an abstract data type that can store certain values, without any particular order, and no repeated values. It is a computer implementation of the mathematical concept of a finite Set. From Wikipedia

The Set data structure is usually used to test whether elements belong to set of values. Rather then only containing elements, Sets are more used to perform operations on multiple values at once with methods such as `union`, `intersect`, etc…

## Complexity

Average
Access Search Insertion Deletion
- O(n) O(n) O(n)

To get a full overview of the time and space complexity of the Set data structure, have a look to this excellent Big O cheat sheet.

## The code

``````function Set() {
this.values = [];
this.numberOfValues = 0;
}

if(!~this.values.indexOf(value)) {
this.values.push(value);
this.numberOfValues++;
}
};
Set.prototype.remove = function(value) {
var index = this.values.indexOf(value);
if(~index) {
this.values.splice(index, 1);
this.numberOfValues--;
}
};
Set.prototype.contains = function(value) {
return this.values.indexOf(value) !== -1;
};
Set.prototype.union = function(set) {
var newSet = new Set();
set.values.forEach(function(value) {
});
this.values.forEach(function(value) {
});
return newSet;
};
Set.prototype.intersect = function(set) {
var newSet = new Set();
this.values.forEach(function(value) {
if(set.contains(value)) {
}
});
return newSet;
};
Set.prototype.difference = function(set) {
var newSet = new Set();
this.values.forEach(function(value) {
if(!set.contains(value)) {
}
});
return newSet;
};
Set.prototype.isSubset = function(set) {
return set.values.every(function(value) {
return this.contains(value);
}, this);
};
Set.prototype.length = function() {
return this.numberOfValues;
};
Set.prototype.print = function() {
console.log(this.values.join(' '));
};

var set = new Set();
set.print(); // => 1 2 3 4
set.remove(3);
set.print(); // => 1 2 4
console.log('contains 4 is true:', set.contains(4)); // => true
console.log('contains 3 is false:', set.contains(3)); // => false
console.log('---');
var set1 = new Set();
var set2 = new Set();