Skip to content

Instantly share code, notes, and snippets.

@petergi
Created December 29, 2023 06:20
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 petergi/b14d22cc46111907dd3bafc051b3489e to your computer and use it in GitHub Desktop.
Save petergi/b14d22cc46111907dd3bafc051b3489e to your computer and use it in GitHub Desktop.
Calculates the difference between two arrays, without filtering duplicate values.
// 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