Created
July 2, 2013 14:46
-
-
Save benjamingr/5909909 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Here is a solution using ES6 sets. | |
var a = new Set(Array1); | |
Array2.forEach(function(elem){ | |
a.delete(elem); | |
}); | |
return a; | |
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
Here's the general idea of the algorithm:
Array1
.className
to a truth mapArray2
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) aclassName
, and each value istrue
.