An unordered and unique data structure

Es6 has the Set Set

Common operations on sets: de-duplication, determining whether an element is in the set, and finding the intersection

Definition 1.

A collection is made up of an unordered and unique (that is, unrepeatable) set of items

2. Specific operations

create

class Set {
	constructor() {
		this.items = {};  // Use objects to simulate}}Copy the code

methods

  • Has (Element) : Returns true if the element is in the collection, false otherwise

    has(element) {
        return element in items;
    }
    Copy the code
    has(element) {
        return Object.prototype.hasOwnProperty.call(this.item, element);
    }
    Copy the code
  • Add (Element) : Adds a new element to the collection

    add(element) {
        if(!this.has(element)) {
            this.items[element] = element;
            return true;
        }
        return false;
    }
    Copy the code
  • Delete (Element) : Removes an element from the collection

    detele(element) {
        if(this.has(element)) {
            detele this.items[element];
            return true;
        }
        return false;
    }
    Copy the code
  • Clear () : Removes all elements from the collection

    clear() {
        this.items = {};
    }
    Copy the code
  • Size () : Returns the number of elements contained in the collection. It is similar to the length property of an array

    Method 1: Just like any other data structure, use length directly

    Method 2:

    size() {
        return Object.keys(this.items).length;
    }
    Copy the code

    Method 3:

    sizeLegacy() {
        let count = 0;
        for(let key in this.items) {
            if(this.items.hasOwnProperty(key)){ count++; }}return count;
    }
    Copy the code
  • Values () : Returns an array containing all the values(elements) in the collection

    values() {
        return Object.values(this.items);
    }
    Copy the code

    But object.values () is added to ECMAScript 2017 and applies to modern browsers, equivalent to:

    valuesLegacy() {
        let values = [];
        for(let key in this.items) {
            if(this.items.hasOwnProperty(key)) { values.push(key); }}return values;
    }
    Copy the code

Use 3.

const set = new Set(a); 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.delete(1);
console.log(set.values()); / / [2]
set.delete(2);
console.log(set.values()); / / []
Copy the code
/ / to heavy
const arr = [1.1.2.2];
const arr2 = [...new Set(arr)];
Copy the code

4. Operation of sets

And set

union(otherSet) {
    const unionSet = new Set(a);this.values().forEach(value= > unionSet.add(value));
    otherSet.values().forEach(value= > unionSet.add(value));
    return unionSet;
}
Copy the code

intersection

intersection(otherSet) {
    const intersectionSet = new Set(a);const 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

Let’s optimize it by iterating over the one with fewer elements in AB

intersection(otherSet) {
    const intersectionSet = new Set(a);const values = this.values(); 
    const otherValues = otherSet.values();
    let biggerSet = values; 
    let smallerSet = otherValues; 
    if (otherValues.length - values.length > 0) { 
        biggerSet = otherValues;
        smallerSet = values;
    }
    smallerSet.forEach(value= > { // Fewer elements have fewer iterations
        if(biggerSet.includes(value)) { intersectionSet.add(value); }});return intersectionSet;
}
Copy the code

Difference set

difference(otherSet) {
    const differenceSet = new Set(a);this.values().forEach(value= > {
        if(!otherSet.has(value)) {
            differenceSet.add(value);
        }
    });
    return differenceSet;
}
Copy the code

A subset of

isSubsetOf(otherSet) {
    if(this.size() > otherSet.size()) {
        return false;
    }
    let isSubset = true;
    this.values().every(value= > {
        if(! otherSet.has(value)){ isSubset =false;
            return false;
        }
        return true;
    });
    return isSubset;
}
Copy the code

5. Front ends and collections

let mySet = new Set(a);let o = {a:1.b:2};
mySet.add(o)
mySet.add({a:1.b:2});
mySet.add('text');

const has = mySet.has(o);

mySet.delete(o)

for(let item of mySet.keys()) console.log(item);
for(let item of mySet.value()) console.log(item);
for(let [key, value] of mySet.keys()) console.log(key, value);

const myArr =[...mySet];
const myArr = Array.from(mySet);

const mySet2 = new Set([1.2.3.4]);
const intersection = new Set([..mySet].filter(x= > mySet2.has(x)));
const difference = new Set([..mySet].filter(x= >! mySet2.has(x)));Copy the code