Skip to content

Commit

Permalink
Improve difference time complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
cuchi committed Jul 9, 2018
1 parent d6558dd commit 2d1a225
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions source/difference.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _includes from './internal/_includes';
import _curry2 from './internal/_curry2';
import _Set from './internal/_Set';


/**
Expand All @@ -26,8 +26,15 @@ var difference = _curry2(function difference(first, second) {
var out = [];
var idx = 0;
var firstLen = first.length;
var secondLen = second.length;
var toFilterOut = new _Set();

for (var i = 0; i < secondLen; i += 1) {
toFilterOut.add(second[i]);
}

while (idx < firstLen) {
if (!_includes(first[idx], second) && !_includes(first[idx], out)) {
if (toFilterOut.add(first[idx])) {
out[out.length] = first[idx];
}
idx += 1;
Expand Down

0 comments on commit 2d1a225

Please sign in to comment.