Skip to content

Instantly share code, notes, and snippets.

@benjamingr
Created July 2, 2013 14:46
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 benjamingr/5909909 to your computer and use it in GitHub Desktop.
Save benjamingr/5909909 to your computer and use it in GitHub Desktop.
Here is a solution using ES6 sets.
var a = new Set(Array1);
Array2.forEach(function(elem){
a.delete(elem);
});
return a;
@Zirak
Copy link

Zirak commented Jul 2, 2013

Here's the general idea of the algorithm:

  • Iterate over Array1.
    • Add each item's className to a truth map
  • Iterate over Array2
    • If the item's className is not in the truth map, add it to the result.

That simple, O(n+m). By a truth map, my intention is a plain js object, where each key is (in this case) a className, and each value is true.

@benjamingr
Copy link
Author

That assumes that adding properties and removing properties from a JS object is O(1), but it works :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment