Set
Last updated
Was this helpful?
Last updated
Was this helpful?
Sets are a type of associative containers in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element -
Add: Insert an element in the set if present not.
Delete: Remove an element from the set.
Has : Return true if the an element is present or else return false.
Size: Return Size of the Set.
isEmpty : Check if the set is empty if empty return true else false.
Union: Return new Set which contains all the elements from two sets.
Intersection: Return new Set which contains the intersecting element from two sets.
Difference: Return new Set only containing the elements which are not present in other sets.
isSubset: Return true if all elements are present in the given otherSet.
Set Only Contains Unique Elements in it.
Let start by defining an class name Set that has one property, items which will hold the elements in the set.we are using objects to store elements in the set instead, you can also use an array.
While inserting an element into the Set, we first need to check if it already exists or not. By using has a method. 1. if the element is already present
Return false
Else insert an element into the Set.
Set items property key and value as an element.
Remove an element from the set.
Check if the element is already present
If not present return false.
Else delete the element from the items property.
```javascript
delete(element) { if (this.has(element)) { delete this.items[element]; return true; } return false; }
The union of the sets A and B, denoted by A ∪ B. It is set only contains distinct elements from set A or set B or both.
otherSet Must be an Instance of Set if not throw an error.
Define a new Union Set.
Loop both the Sets and add elements into the Union Set if not present.
The intersection of the sets A and B, denoted by A ∩ B, is the Set of elements belongs to both A and B, only common elements.
otherSet Must be an Instance of Set if not throw an error.
Define a new Intersection Set.
Loop the Set and add elements into the Intersection Set if and only if, the elements are present in both the Sets.
The difference between sets A and B is denoted by A – B. Only containing elements of set A but not in B.
otherSet Must be an Instance of Set if not throw an error.
Define a new Difference Set.
Loop the Set and add elements into the Difference Set which are not common in otherSet
A is a subset of B, denoted by A ⊆ B or equivalently.if only if all the elements of A is present in B.
otherSet Must be an Instance of Set if not throw an error.
Methods
Complexity
Add
O(n)
Delete
O(1)
Has
O(n)
Check if the element already exists in the set or not. You can loop until the entire the items and compare the given element with the set elements. If a match is found then return true or else false. Or you can javascript built-in method of
Set Data Structure was also introduced in the ES6, javascript all the methods defined until know is present in Standard ES6 .
In mathematics, a [set]()) also has some basic operations such as union, intersection, and difference.
Loop the otherSet check if all the elements are present or not or use method.
you get the full source