I have solved this challenge with 2 for loops with too much time complexity. I want to understand how to convert this code into a higher-order function and use the filter in it, please explain that in detail? I want to make the code shorter and optimize as well
function diffArray(arr1, arr2) {
var newArr = [];
var count=0;
var ar=arr1.concat(arr2);
ar= ar.sort();
//console.log(ar);
for(var i=0;i<ar.length;i++)
{
for(var j=0;j<ar.length;j++)
{
if(ar[i]==ar[j])
{
//console.log(ar[i]+" == "+ar[j] +" for if count" + " value of count "+ count);
count+=1;
}
else
{ //console.log("for else");
}
}
if(count<2)
{
//console.log("Value of count <2 "+ count)
newArr.push(ar[i]);
}
count=0;
}
// console.log(newArr);
return newArr;
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);