Skip to content

Instantly share code, notes, and snippets.

@mstfsnc
Last active July 19, 2023 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstfsnc/f6385d098092d9b14a945cb746ed9b20 to your computer and use it in GitHub Desktop.
Save mstfsnc/f6385d098092d9b14a945cb746ed9b20 to your computer and use it in GitHub Desktop.
symmetric difference
const first = ["A", "B", "C"]
const second = ["A", "B"]
// 1) long way
const firstArrayDiff = first.filter((f) => {
return second.includes(f) === false
})
const secondArrayDiff = second.filter((s) => {
return first.includes(s) === false
})
if (firstArrayDiff.length || secondArrayDiff.length) {
// something different
console.log("long diff");
}
// 2) short way
const totalDiff = [...first.filter(f => !second.includes(f)), ...second.filter(s => !first.includes(s))]
if (totalDiff.length) {
// something different
console.log("short diff");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment