Since in there is no such data structure as a HashSet in Google Apps Script yet, this is a somewhat tricky problem to solve efficiently. You can opt for the quadratic solution, which for each item in the first range would iterate over the whole second one trying to find a 'match', in order to discard it. This would look somewhat like @Cooper 's solution.
As an alternative, and considering that Google Apps Script's objects preserve property insertion order, you can use the following code which should theoretically yield better performance results (specially for larger workloads).
function DIFFERENCE(range1, range2) {
var o = {};
for (var i=0; i<range1.length; i++) {
for (var j=0; j<range1[0].length; j++) {
if (!o[range1[i][j]])
o[range1[i][j]] = true;
}
}
for (var i=0; i<range2.length; i++) {
for (var j=0; j<range2[0].length; j++) {
if (o[range2[i][j]])
o[range2[i][j]] = false;
}
}
return Object.keys(o).filter(function f(key) { return o[key]; }).map(function(res) { return parseFloat(res) });
}
This function assumes that you are dealing with numbers. If you want it to work with strings as well, you can replace the last line of code by the following: return Object.keys(o).filter(function f(key) { return o[key]; });
You can also see a couple of examples here:
{1;2}
and{1,2}
are valid arrays which are equal to JavaScript[[1],[2]]
and[[1,2]]
respectively. – TheMaster