Created
December 29, 2023 06:20
-
-
Save petergi/b14d22cc46111907dd3bafc051b3489e to your computer and use it in GitHub Desktop.
Calculates the difference between two arrays, without filtering duplicate values.
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
// 17% faster than using filter on set | |
function difference(a, b) { | |
const s = new Set(b) | |
const result = [] | |
for (const x of a) { | |
if (!s.has(x)) { | |
result.push(x) | |
} | |
} | |
return result | |
} | |
difference([1, 2, 3, 3], [1, 2, 4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment