Last active
March 29, 2023 11:40
-
-
Save alexalannunes/68a27c7b3caa2a48bcb93bf80e2d34b9 to your computer and use it in GitHub Desktop.
array intersection, diff, symmetrical 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 a = [1,2,3] | |
const b = [1,4,5] | |
// using find instead includes | |
const inter = a.filter(x => b.find(x1 => x1 == x)); | |
const diff = a.filter(x => !b.find(x1 => x1 == x)); | |
const simtricDiff = a.filter(x => !b.find(x1 => x1 == x)).concat(b.filter(x => !a.find(x1 => x1 == x))); | |
inter // [ 1 ] | |
diff // [ 2, 3 ] | |
simetricDiff // [ 2, 3, 4, 5 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment