Javascript - Set

1 - About

Collection - Set in Javascript works only with primitive type as there is no way to define an object equality function.

3 - Example

  • Declaration

let colorSet = new Set();

  • Add/Delete

colorSet.add("blue");
colorSet.add("blue");
colorSet.add("red");
colorSet.add("orange");
colorSet.delete("blue");

  • Size

console.log("Set size should be 2: "+colorSet.size);

  • Contains (has)

console.log("Has color red ? "+colorSet.has("red"));


console.log("Color iteration:");
for (let color of colorSet) console.log("    * "+color);

  • Keys iteration

console.log("Colors Key:");
for (let colorKey of colorSet.keys()) console.log("    * "+colorKey);

  • Values iteration

console.log("Colors Values:");
for (let colorValue of colorSet.values()) console.log("    * "+colorValue);

4 - Interface

Interface definition in declaration file


interface Set<T> {
    add(value: T): this;
    clear(): void;
    delete(value: T): boolean;
    forEach(callbackfn: (value: T, value2: T, set: Set<T>) => void, thisArg?: any): void;
    has(value: T): boolean;
    readonly size: number;
}

5 - Management

5.1 - Declaration


let colorSet = new Set();


let numberSet = new Set([1, 2, 3, 4])

5.1.1 - to Array

to an array with the spread operator


let numberArray = [...numberSet];

  • Array.from

let myArr = Array.from(mySet)

5.2 - Add


colorSet.add("blue");

5.3 - Delete

  • Delete

colorSet.delete("blue");

5.4 - Contains (has)


console.log("Has color red ? "+colorSet.has("red"));

5.5 - Iteration

5.5.1 - for of

In a set, the key is also the value With for of:

  • Key Color iteration

console.log("Color iteration:");
for (let color of colorSet) console.log("    * "+color);

  • Keys iteration explicit

console.log("Colors Key:");
for (let colorKey of colorSet.keys()) console.log("    * "+colorKey);

  • Values iteration

console.log("Colors Values:");
for (let colorValue of colorSet.values()) console.log("    * "+colorValue);

5.5.2 - foreach


let mySet = new Set([1,2,3,4,1]);
mySet.forEach(function(value) {
  console.log(value)
})

5.5.3 - Functional Programming

To use functional programming, you need to transform the set in an array.

Example:


let mySet = new Set([1,2,3,4,1]);
[...mySet].filter(e => e!=2).map(e => console.log(e));

5.6 - Intersect

Set - Intersect


set1 = new Set([1, 2, 3, 4])
set2 = new Set([2, 4])
let intersection = new Set([...set1].filter(x => set2.has(x)))
for (let element of intersection ) console.log(element );

5.7 - Diff

Set - Difference

  • Asymmetric difference (where the first set is the driver)

function difference(setA, setB) {
    return new Set([...setA].filter(x => !setB.has(x)))
    // Longer version
    // let _difference = new Set(setA)
    // for (let elem of setB) {
    //     _difference.delete(elem)
    // }
    // return _difference
}

  • symmetric difference implementation (where the difference on the two set is shown)

function symmetricDifference(setA, setB) {
    let _difference = new Set(setA)
    for (let elem of setB) {
        if (_difference.has(elem)) {
            _difference.delete(elem)
        } else {
            _difference.add(elem)
        }
    }
    return _difference
}

  • The set

let setA = new Set([1, 2, 3, 4])
let setB = new Set([3, 4, 5, 6])

let diff = difference(setA,setB);
console.log("The difference is");
for (let e of diff ) console.log(" * "+e);

let symDiff = symmetricDifference(setA,setB);
console.log("The asymmetric difference is");
for (let e of symDiff ) console.log(" * "+e);

5.8 - Equality

Logical Data Modeling - Equivalence Relationship (Equality)

  • Implementation of an asymmetric equality.

function areEquals(setA, setB){
  if (setA.size != setB.size) {
    return false;
  } else {
    let difference = new Set([...setA].filter(x => !setB.has(x)))
    return difference.size == 0;
  }
}

  • Order does not matter

set1 = new Set([1, 2])
set2 = new Set([2, 1])
console.log("Order does not matter: "+areEquals(set1,set2));

  • False

set1 = new Set([1])
set2 = new Set([1,2])
console.log("Equality is symmetric. Diff is : "+areEquals(set1,set2));

  • Output

6 - Documentation / Reference


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap