Intermediate Algorithm Scripting: Diff Two Arrays (pseudocode to javascript)

I am doing this challenge which tells you to only gives out the elements that do not co-exist in two given arrays.
I am trying to solve this problem using these steps/logics.

  1. concat two array into one array newArray
  2. filter out all the array elements as long as it appeared more than once.

I tried out this array and find that if you have duplicates inside your array, the position of the element will not be updated once it appeared inside an array. See [3] is both position 2 from the console.

so here is my “solution” with some pseudocode in it

var arr = [“hello”,“world”,3,4,5,62,3,12];

for(let i = 0; i < arr.length; i++){
if(/arr.indexOf(arr[i]) appeared more than once/){
arr.splice(i,1);
}
}

console.log(arr);

how do I convert that pseudocode to javascipt?
am I overly complicating things here?

It’s not that the position is not being updated, what’s happening is that you’re searching the indexOf 3, it will return the index of the first 3 it finds, which is in the index 2.

You’re in the right path by concatenating both arrays, but instead of looking for duplicated elements in that big array, I think it’s easier and simpler if you iterate the big array and check if the element is present in both arr1 and arr2.

1 Like

thanks for the suggestion, I solved the algorithm.