You can do it in a single loop if you use separate indices:
const diffArray = (arr1, arr2) => { const difference = [] let index1 = 0 let index2 = 0 while (index1 < arr1.length || index2 < arr2.lenght) { if (arr1[index1] === arr2[index2]) { index1++ index2++ } else if (arr1.length >= arr2.lenght) { difference.push(arr1[index1++]) } else { difference.push(arr2[index2++]) } } return difference }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You can do it in a single loop if you use separate indices: