Skip to content

Commit

Permalink
Adding Set.symmetricDifference
Browse files Browse the repository at this point in the history
  • Loading branch information
Yomguithereal committed Apr 13, 2017
1 parent 4d118a3 commit 94c1194
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
30 changes: 30 additions & 0 deletions set.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,33 @@ exports.difference = function(A, B) {

return D;
};

/**
* Function computing the symmetric difference between two sets.
*
* @param {Set} A - First set.
* @param {Set} B - Second set.
* @return {Set} - The symmetric difference.
*/
exports.symmetricDifference = function(A, B) {
var S = new Set();

var iterator = A.values(),
step;

while ((step = iterator.next(), !step.done)) {
if (!B.has(step.value))
S.add(step.value);
}

iterator = B.values();

while ((step = iterator.next(), !step.done)) {
if (!A.has(step.value))
S.add(step.value);
}

return S;
};

// TODO: subset, superset, in-place
12 changes: 12 additions & 0 deletions test/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,16 @@ describe('Set functions', function() {
assert.deepEqual(Array.from(D), [1, 4, 5]);
});
});

describe('#.symmetricDifference', function() {

it('should properly compute the symmetric difference of two sets.', function() {
var A = new Set([1, 2, 3]),
B = new Set([3, 4, 5]);

var S = functions.symmetricDifference(A, B);

assert.deepEqual(Array.from(S), [1, 2, 4, 5]);
});
});
});

0 comments on commit 94c1194

Please sign in to comment.