I am facing a challenge with arrays. Let's say I have an initialArray = [1,2,3,4,5,6]
My goal is to add another array [1,2,3]
to it.
1 / If the array [1,2,3]
already exists in the initialArray
, I need to remove it.
The expected result at this stage would be [4,5,6]
2 / Next, I want to add the array [1,2,3]
again. Since it was removed earlier, it should now be added to the end of the initialArray
.
So after this step, the resulting array will be [4,5,6,1,2,3]
3 / Now, I aim to add the array [1,2,3,4,5,6]
. In this scenario, all elements are already present, so the array should become empty []
I have attempted to use the .filter()
method to remove existing values successfully, but I am struggling to concatenate arrays when certain values do not exist. How can I achieve both tasks efficiently?