Constructor
new _Set()
In computer science, 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. Unlike most other collection
types, rather than retrieving a specific element from a set, one typically tests a value for
membership in a set. Full wikipedia article at:
https://en.wikipedia.org/wiki/Set_(abstract_data_type)
Example
const _Set = require('dstructures')._Set;
const mySet = new _Set();
Methods
add(element) → {Boolean}
Adds element to a set.
Parameters:
Name | Type | Description |
---|---|---|
element |
any
|
Given element. |
Returns:
- Type:
-
Boolean
Returns false if the given element is already member of the set, otherwise returns true.
Example
[] Set.add(1); // [1]
[1] Set.add(2); // [1, 2]
conatains(element) → {Boolean}
Checks if given element is member of the set.
Parameters:
Name | Type | Description |
---|---|---|
element |
any
|
Given element. |
Returns:
- Type:
-
Boolean
Returns true if the given element is member of the set, otherwise returns false.
Example
[1, 2, 3] Set.contains(3); // true
[1, 2, 3] Set.contains('cat'); // false
difference(set)
Implements difference of two sets.
Parameters:
Name | Type | Description |
---|---|---|
set |
_Set
|
Given set. |
Returns:
Returns set difference of the two sets.
Returns false if there is no argument passed to the function or the argument is not of type Set.
Example
[1, 2, 3] Set1 & [1, 2, 4] Set2 Set1.difference(Set2); // [4] Set
display() → {Array}
Returns the array representation of a set.
Returns:
- Type:
-
Array
Returns the array representation of set.
Example
[1, 2, 3] Set.display(); // [1, 2, 3]
intersect(set)
Implements intersection of two sets.
Parameters:
Name | Type | Description |
---|---|---|
set |
_Set
|
Given set. |
Returns:
Returns set intersection of the two sets.
Returns false if there is no argument passed to the function or the argument is not of type Set.
Example
[1, 2, 3] Set1 & [4, 2, 6] Set2 Set1.intersect(Set2); // [2] Set
remove(element) → {Boolean}
Deletes element from a set.
Parameters:
Name | Type | Description |
---|---|---|
element |
any
|
Given element. |
Returns:
- Type:
-
Boolean
Returns false if the given element is not present, otherwise returns true.
Example
[1, 2, 3] Set.remove(2); // [1, 3]
[1, 3] Set.remove(1); // [3]
size() → {Number}
Returns the size of the set.
Returns:
- Type:
-
Number
Returns the size of the set.
Example
[1, 2] Set.size(); // 2
subset(set)
Cheks if the set is subset of a given set.
Parameters:
Name | Type | Description |
---|---|---|
set |
_Set
|
Given set. |
Returns:
Returns true if the set is subset of the given set, otherwise returns false.
Returns false if there is no argument passed to the function or the argument is not of type Set.
Example
[1, 2] Set1 & [1, 2, 3] Set2 Set1.subset(Set2); // true
union(set)
Implements union of two sets.
Parameters:
Name | Type | Description |
---|---|---|
set |
_Set
|
Given set. |
Returns:
Returns set union of the two sets.
Returns false if there is no argument passed to the function or the argument is not of type Set.
Example
[1, 2, 3] Set1 & [4, 5, 6] Set2 Set1.union(Set2); // [1, 2, 3, 4, 5, 6] Set