So I've been trying to remove numbers of an arrayA that are in an arrayB. But if a number is only one time in that arrayB, and more than one time in that arrayA, i want my function to remove only ONE of them.
My function looks like it could work but it doesn't...
the expected output is : 1,3,3,4,5
let arrayA = [1,1,2,3,3,3,4,4,5]
let arrayB = [1,2,3,4]
function remove(arrayB,arrayA){
//newarray is the result array i want to get
let newarray = [];
//counter will controll if a number is more than one time in my arrayA
let counter = [];
arrayA.forEach(function(n){
//if a number of my arrayA is not in my arrayB
if(arrayB.indexOf(n) == -1){
newarray.push(n);
}
//if a number of my arrayB is only one time in my arrayA
else if(a.indexOf(n) == a.lastIndexOf(n)){
}
//if a number is more than one time but its the first one we meet
else if(a.indexOf(n) !== a.lastIndexOf(n) && counter.includes(n) == false){
//push it into the counter array so we'll know we already had this number
counter.push(n)
}
// if it's the second time we have to keep it and get it in the newarray
else {
newarray.push(n);
}
})
document.write(newarray)
}