- Notifications
You must be signed in to change notification settings - Fork55
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
varset=newSet();//an empty set
This method insert the element in the set. The element must be created with the correct declaration as you see in the example.
varset=newSet();vare1=newElement(0);vare2=newElement(1);set.insert(e1);//set contains e1set.insert(e2);//set contains e1 e2
This method insert the element in the set. The element must be created with the correct declaration as you see in the example.
varset=newSet();vare1=newElement(0);vare2=newElement(1);set.multiInsert([e1,e2]);//set contains e1 e2
This method returns the set representing the union of the two sets.
varset1=newSet();varset2=newSet();vare1=newElement(0);set1.insert(e1);vare2=newElement(1);set2.insert(e2);varunion=set1.union(set2);//union contains e1 e2
n: the number of total elements stored in the sets.
This method returns the set representing the intersection of the two sets.
varset1=newSet();varset2=newSet();vare1=newElement(0);set1.multiInsert([e1,e2]);vare2=newElement(1);set2.insert(e2);varintersect=set1.intersect(set2);//intersection contains e2
n: the number of elements stored in the first set.m: the number of elements stored in the second set.
This method returns the set representing the difference of the first set with the second.
varset1=newSet();varset2=newSet();vare1=newElement(0);set1.multiInsert([e1,e2]);vare2=newElement(1);set2.insert(e2);vardifference=set1.difference(set2);//difference contains e1
n: the number of elements stored in the first set.m: the number of elements stored in the second set.
This method returns the set representing the cartesian product of the first set with the second.
varset1=newSet();varset2=newSet();vare1=newElement(0);set1.insert(e1);vare2=newElement(1);set2.insert(e2);varproduct=set1.cartesianProduct(set2);//product contains [e1, e2]
n: the number of elements stored in the first set.m: the number of elements stored in the second set.
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.```JavaScriptvar set = new Set();var e1 = new Element(0);var e2 = new Element(1);set.multiInsert([e1, e2]);set.getCardinality(); //2
This method checks if the set is empty or not.
varset=newSet();vare1=newElement(0);vare2=newElement(1);set.isEmpty();//falseset.multiInsert([e1,e2]);set.isEmpty();//true
This method returns a clone of the set.
varset=newSet();vare1=newElement(0);vare2=newElement(1);varclone=set.clone();//clone contains e1, e2