This is the 19th day of my participation in the Genwen Challenge
What is a set
A collection is made up of an unordered and unique (that is, unrepeatable) set of items. You can also think of a set as an array with no repeating elements and no notion of order.
Implement collection
We use objects to represent collections. Use objects to implement collections, because javaScript objects don’t allow a key to point to two different properties, so the elements in a collection are guaranteed to be unique. Some methods of collection:
- Add (value): Adds a new item to the collection.
- Remove (value): Removes a value from the collection.
- Has (value): Returns true if the value is in the collection, false otherwise.
- Clear (): Removes all items in the collection.
- Size (): Returns the number of elements contained in the collection. Similar to the length property of an array.
- Values (): Returns an array containing all the values in the collection.
Based on es6’s implementation of the Set class:
function Set () {
let items = {}
/ / from the method
this.has = function (value) {
return items.hasOwnProperty(value)
}
this.add = function (value) {
// Check if it already exists before adding it
if(!this.has(value)){
items[value] = value
return true
}
return false
// Return true for successful addition, false for failed addition
}
this.remove = function (value) {
// Check if the value to be removed exists in the collection
if(this.has(value)) {
delete items[value]
return true
}
return false
}
this.clear = function () {
items = {}
}
this.size = function () {
return Object.keys(items).length
}
this.values = function () {
return Object.keys(items)
}
}
Copy the code
Using the collection
let set = new Set()
set.add(1)
console.log(set.values()) / / / '1'
console.log(set.has(1)) // true
console.log(set.size()) / / 1
set.add(2)
console.log(set.values()) / / / '1', '2'
console.log(set.has(2)) // true
console.log(set.size()) / / 2
set.remove(1)
console.log(set.values()) / / / '2'
Copy the code
Operation of set
- Union: For a given set, returns a set containing two setsAll the elementsA new set of.
- Intersection: For a given set, returns a set containing two setsThere are elementsA new set of.
- Difference set: For a given set of two sets, return a new set containing all elements that exist in the first set and not in the second.
- Subset: Verifies whether a given set is a subset of another set.
Implementation of union
// The end result of a union is to return a new collection
this.union = function (otherSet) {
let unionSet = new Set(a)// Get all the values of the set, iterate over all the values to add to the newly declared set
let values = this.values()
for (let i = 0; i < values.length; i++) {
unionSet.add(values[i])
}
// Get all the values of the passed collection, iterate over all the values to add to the newly declared collection
values = othereSet.values()
for (let i = 0; i < values.length; i++) {
unionSet.add(values[i])
}
return unionSet
}
Copy the code
Realization of intersection
// The end result of the intersection is to return a new set
this.intersection = function (otherSet) {
let intersectionSet = new Set(a)// Get all values of the set, iterate over each item and add items that also exist in the otherSet to the new set
let values = this.values()
for (let i = 0; i < values.length; i++) {
if(otherSet.has(values[i])){
intersectionSet.add(values[i])
}
}
return intersectionSet
}
Copy the code
The realization of difference set
// The end result of the difference set is to return a new set
this.difference = function (otherSet) {
let differenceSet = new Set(a)// Get all the values of the set, iterate over each item and add the items that do not exist in the otherSet to the new set
let values = this.values()
for (let i = 0; i < values.length; i++) {
if(! otherSet.has(values[i])){ differenceSet.add(values[i]) } }return differenceSet
}
Copy the code
The implementation of A subset determines whether A is A subset of B
// Subset returns a Boolean value
this.subset = function (otherSet) {
If the number of elements in the current set is greater than the number of elements in the passed set, it must not be a subset of the set. Return false
if(this.size() > otherSet.size()) {
return false
} else {
let values = this.values()
// Iterates through all the elements of the current collection, returning false if they do not exist in the otherSet collection
for(let i = 0; i < values.length; i++) {
if(! otherSet.has(values[i])){return false}}return true}}Copy the code