Depends on your definition of good.
As always.
And I'm not sure it is the same
question as the array contents are specified to be numbers only,
despite some of the odd solutions that followed:-
I didn't see either from the OP of this thread or the original
question on the linked document anything that implied the sets
contained only numbers. And one of the early examples in the linked
document in fact contained a mix of numbers and strings.
It was odd, though that the OP specified the logical notion of sets
(for which there is no notion of order/position) and the
implementation of arrays (for which order/position is inherent.)
minusAB : function (a, b) {
var h = {};
b.each(function (v) { h[v] = true; });
return a.filter(function (v) { return !h.hasOwnProperty(v); });
},
My own solution kept with the arrays:
function setDiff(A, B) {
var C = [];
A.forEach(function(el) {if (B.indexOf(el) < 0) C.push(el);});
return C;
}
I originally pointed to the document out of concern that Thomas Lahn
had it right that this was a homework problem. There is some
pedagogical benefit to the discussions on that page that wouldn't be
found just by supplying an answer here. However, knowing that I've
had to solve that problem in the real world several times, and
knowing
that the OP has been posting programming questions in various forums
for years, in retrospect, it seems likely that it wasn't a homework
problem.
-- Scott