Skip to content
Battistella Stefano edited this page May 20, 2014 · 1 revision

In computer science, a set is an abstract data structure 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. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.

Wikipedia

var set = new Set(); //an empty set

Methods

insert(element)

This method insert the element in the set. The element must be created with the correct declaration as you see in the example.

var set = new Set();
var e1 = new Element(0);
var e2 = new Element(1);
set.insert(e1); //set contains e1
set.insert(e2); //set contains e1 e2

Complexity: O(1)

multiInsert(elements)

This method insert the element in the set. The element must be created with the correct declaration as you see in the example.

var set = new Set();
var e1 = new Element(0);
var e2 = new Element(1);
set.multiInsert([e1, e2]); //set contains e1 e2

Complexity: O(n)

union(set)

This method returns the set representing the union of the two sets.

var set1 = new Set();
var set2 = new Set();
var e1 = new Element(0);
set1.insert(e1);
var e2 = new Element(1);
set2.insert(e2);
var union = set1.union(set2); //union contains e1 e2

Complexity: O(n)

n: the number of total elements stored in the sets.

intersect(set)

This method returns the set representing the intersection of the two sets.

var set1 = new Set();
var set2 = new Set();
var e1 = new Element(0);
set1.multiInsert([e1, e2]);
var e2 = new Element(1);
set2.insert(e2);
var intersect = set1.intersect(set2); //intersection contains e2

Complexity: O(n·m)

n: the number of elements stored in the first set. m: the number of elements stored in the second set.

difference(set)

This method returns the set representing the difference of the first set with the second.

var set1 = new Set();
var set2 = new Set();
var e1 = new Element(0);
set1.multiInsert([e1, e2]);
var e2 = new Element(1);
set2.insert(e2);
var difference = set1.difference(set2); //difference contains e1

Complexity: O(n·m)

n: the number of elements stored in the first set. m: the number of elements stored in the second set.

cartesianProduct(set)

This method returns the set representing the cartesian product of the first set with the second.

var set1 = new Set();
var set2 = new Set();
var e1 = new Element(0);
set1.insert(e1);
var e2 = new Element(1);
set2.insert(e2);
var product = set1.cartesianProduct(set2); //product contains [e1, e2]

Complexity: O(n·m)

n: the number of elements stored in the first set. m: the number of elements stored in the second set.

getItems()

This method returns the items stored in the set. var set = new Set(); var e1 = new Element(0); var e2 = new Element(1); set.multiInsert([e1, e2]); set.getItems(); //0, 1

#### Complexity: _O_(n)

### getCardinality()
This method returns the size of the set.
```JavaScript
var set = new Set();
var e1 = new Element(0);
var e2 = new Element(1);
set.multiInsert([e1, e2]);
set.getCardinality(); //2

Complexity: O(1)

isEmpty()

This method checks if the set is empty or not.

var set = new Set();
var e1 = new Element(0);
var e2 = new Element(1);
set.isEmpty(); //false
set.multiInsert([e1, e2]);
set.isEmpty(); //true

Complexity: O(1)

clone()

This method returns a clone of the set.

var set = new Set();
var e1 = new Element(0);
var e2 = new Element(1);
var clone = set.clone(); //clone contains e1, e2

Complexity: O(n)

Clone this wiki locally