Last active
July 19, 2023 19:48
-
-
Save mstfsnc/f6385d098092d9b14a945cb746ed9b20 to your computer and use it in GitHub Desktop.
symmetric difference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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