I have two string arrays (always will contain string values)
var origArr = ['b', 'c', 'a'];
var currentArr = ['d', 'a', 'e', 'c'];
And I need two functions: one that finds what has been added (d, e) and one that finds what has been removed (b). I have a function that finds all the differences, but I cannot figure out how to do this. Also efficiency is important and these arrays might contain over 1000 values and they are not sorted. Lastly, it shouldn't fail if one or both of the arrays is empty.
function getArrayDifference(a1, a2) {
var tempArr = [],
diffArr = [];
for (var i = 0; i < a1.length; i++) {
tempArr[a1[i]] = true;
}
for (var j = 0; j < a2.length; j++) {
if (tempArr[a2[j]]) delete tempArr[a2[j]];
else tempArr[a2[j]] = true;
}
for (var k in tempArr) {
diffArr.push(k);
return diffArr;
}
}