If A = (2, 1, 3)
and B = (1, 2, 3, 4, 5, 6, 7)
Then do you want the uncommon elements (i.e., elements not existing in either)? You can try this:
//note: could be improved - wrote it quickly
function uncommon(a, b) {
var map = {};
var alreadyAdded = {};
var uncommonElements = [];
var arrays = (a.length > b.length) ?
{first: b, second: a} :
{first: a, second: b};
for(var i = 0; i < arrays.first.length; i++) {
map[arrays.first[i]] = true;
}
for(var i = 0; i < arrays.second.length; i++) {
if(!map[arrays.second[i]]) {
uncommonElements.push(arrays.second[i]);
alreadyAdded[arrays.second[i]] = true;
}
}
for(var i = 0; i < arrays.first.length; i++) {
if(!map[arrays.second[i]] && !alreadyAdded[arrays.first[i]]) {
uncommonElements.push(arrays.first[i]);
}
}
return uncommonElements;
}
Also note that if you had:
A = (2, 1, 3, 9)
and B = (1, 2, 3, 4, 5, 6, 7)
, you would get (2, 1, 3, 9)
i.e., elements not found in either one.