3 ms·
If you wanted to do O(n) functionally, you could do [1, 1, 2, 3, 4].filter( (obj => x => { if (obj[x]) { return false; }
by _ar7 9y ago
If you wanted to do O(n) functionally, you could do
[1, 1, 2, 3, 4].filter(
(obj => x => {
if (obj[x]) {
return false;
}
obj[x] = true;
return true;
})({}),
);
What's happening is that you're passing an object (hashmap / hashset) into a function that returns a filtering function, and that object is used inside the closure to track the dupes. It's still a pure function because even though you're mutating the passed in object, the filtering function is still deterministic and referentially transparent.