Skip to content

Instantly share code, notes, and snippets.

@jfollmann
Forked from mauricior/setTheory.js
Created June 16, 2021 16:42
Show Gist options
  • Save jfollmann/b9d91a49e5fec42e5e87ace502a640ce to your computer and use it in GitHub Desktop.
Save jfollmann/b9d91a49e5fec42e5e87ace502a640ce to your computer and use it in GitHub Desktop.

Revisions

  1. @mauricior mauricior renamed this gist Jun 16, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @mauricior mauricior created this gist Jun 16, 2021.
    21 changes: 21 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    // Examples of https://medium.com/@alvaro.saburido/set-theory-for-arrays-in-es6-eb2f20a61848
    const arrA = [1, 3, 4, 5];
    const arrB = [1, 2, 5, 6, 7];

    const intersection = arrA.filter((x) => arrB.includes(x));
    console.log('Intersection: ', intersection);
    console.log('Expected: [1,5]');

    const difference = arrA.filter((x) => !arrB.includes(x));
    console.log('\nDifference: ', difference);
    console.log('Expected: [3,4]');

    const symmetricalDifference = arrA
    .filter((x) => !arrB.includes(x))
    .concat(arrB.filter((x) => !arrA.includes(x)));
    console.log('\nSymmetrical Difference: ', symmetricalDifference);
    console.log('Expected: [2,3,4,6,7]');

    const union = [...new Set([...arrA, ...arrB])];
    console.log('\nUnion: ', union);
    console.log('Expected: [1,2,3,4,5,6,7]');