This is the fifth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Set the Set

A Set is an unordered Set of unique (unrepeatable) items. Like the Set class in ES2015, this data structure uses the same mathematical concepts as a finite Set. We will implement a Set class and implement a simple intersection difference Set calculation. Unlike a Map, a Set is a value-value object.

Creating a collection Class

What do you store data in, arrays or objects? We’re using objects, because objects don’t allow a key to point to two values, ensuring that the elements in the set are unique. What methods does he need?

  1. Add (Element): Adds an element to a collection
  2. Delete (Element): Removes an element like a collection
  3. Has (Element): Whether an element exists in the collection
  4. Clear (): Removes all elements from the collection
  5. Size (): Returns all the elements in the collection
  6. Values (): Returns an array containing all the elements of the collection
class Set {
    constructor() {
        this._value = {};
    }
    add(element) {
        if (this.has(element)) return false;

        this._value[element] = element;
        return true;
    }

    delete(element) {
        if (!this.has(element)) return false;

        delete this._value[element];
        return true;
    }

    has(element) {
        return Object.prototype.hasOwnProperty.call(this._value, element)
    }

    clear() {
        this._value = {};
    }

    size() {
        return Object.keys(this._value).length;
    }

    values() {
        return Object.keys(this._value);
    }

}
Copy the code

And set

For a given set, returns a new set containing all the elements of both sets

// union(otherSet) {const result = new Set(); this.values().forEach(element => result.add(element)); otherSet.values().forEach(element => result.add(element)); return result; }Copy the code

intersection

For a given set, returns a new set containing elements common to both sets

// interSection interSection(otherSet) {const result = new Set(); this.values().forEach(element => { if (otherSet.has(element)) { result.add(element) } }); return result; }Copy the code

Difference set

For a given set of two sets, return a new set containing all elements in the first set but not in the second

// Difference (otherSet) {const result = new Set(); this.values().forEach(element => { if (! otherSet.has(element)) { result.add(element) } }); return result; }Copy the code

A subset of

Verify that a given set is a subset of another set

// subset (otherSet) {if (this.size() > otherset.size ()) return false; const values = this.values(); for (let i = 0; i < this.size(); i++) { if (! otherSet.has(values[i])) { return false } } return true; }Copy the code

The test case

const setA = new Set() setA.add(1) setA.add(2) setA.add(3) const setB = new Set() setB.add(1) setB.add(2) setB.add(3) setB.add(4) setB.add(5) console.log(setA.union(setB).values()); // merge 1,2,3,4,5 console.log(setA. InterSection (setB).values()); // Intersection 1, 2, 3 console.log(seta.difference (setB).values()); 【】 console.log(setB); / / the subset of trueCopy the code