Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created December 30, 2016 18:07
Show Gist options
  • Save vlad-bezden/ba5ec89f825b7c94ed753e54144c8abe to your computer and use it in GitHub Desktop.
Save vlad-bezden/ba5ec89f825b7c94ed753e54144c8abe to your computer and use it in GitHub Desktop.
Union, Intersection, and Differences
'use strict'
console.clear()
const array1 = [1, 2, 3]
const array2 = [2, 3, 4]
const union = [...new Set([...array1, ...array2])]
const intersection = [...new Set(array1.filter(a1 => array2.find(a2 => a1 === a2)))]
const differences = [...new Set(array1.filter(a1 => !array2.find(a2 => a1 === a2)))]
console.log(union)
console.log(intersection)
console.log(differences)

Union, Intersection, and Differences

Example on how to get Union, Intersection and Differences from two arrays using Set

A Pen by Vlad Bezden on CodePen.

License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment