This should be a simple algorithm but I can't wrap my head around the logic. I need to take 2 arrays and keep only the values that are found in one array or the other and not both.
For example if I have
arr1 [1,2,3,6]
arr2 [2,3,4,5,7]
I want to have 2 arrays again being
arr1 [1,6]
arr2 [4,5,7]
EDIT:
I removed my old code and put in some other working code. Still not very pretty but at least working. All I would need to do is store arr3 into arr1 and arr4 into arr2.
var arr3 = [];
var arr4 = [];
var foundMatch = false;
//find all arr1 elements that don't match arr2 elements
//store into arr4
for(var i=0; i<arr1.length; i++)
{
foundMatch = false;
for(var j=0; j<arr2.length; j++)
{
if(arr1[i] == arr2[j])
{
console.log("match found for [%s] [%s]", i, j);
foundMatch = true;
break;
}
}
if(!foundMatch)
{
arr4.push(arr1[i]);
}
}
//find all arr2 elements not in arr1
//store in arr3
for(var i=0; i<arr2.length; i++)
{
foundMatch = false;
for(var j=0; j<arr1.length; j++)
{
if(arr2[i] == arr1[j])
{
console.log("match found for [%s] [%s]", i, j);
foundMatch = true;
break;
}
}
if(!foundMatch)
{
arr3.push(arr2[i]);
}
}