Help with the Symmetric Differece

Hy, I’m on the Symmetric Difference Challenge. This is the code I wrote:

function removeFromArr(arr, val) {
  arr.forEach(function (el, i) {
    if (el === val)
      arr.splice(arr.indexOf(el), 1);

function symmetricDiff(a, b) {
  var arr = [];
  arr = a.concat(b);
  a.forEach(function (val, i) {
    b.forEach(function (val1, j) {
      if (val1 === val) removeFromArr(arr, val);
  return arr;

function sym(args) {
  args =;
  return args.reduce(function (a, b) {
    return symmetricDiff(a, b);

But it only passes for two arrays.
The removeFromArr() is a helper function i created to remove elements from the array.
Thanks in advance.

I think there are at least two problems with your code.

  1. removeFromArr doesn’t work as intended: when you remove something from an array while iterating over it you will skip items.
    Why? Let’s say you are in a for loop iterating over an array and your loop counter i is currently 2. Now you remove array[2], thus array[3] becomes the new array[2]. Then the loop continues and i is incremented. Now array[3] is checked, the value that is now in array[2] has been skipped.
    That’s the reason why the case sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) fails: sym([1, 2, 5], [2, 3, 5]) becomes [1, 3]. Then when you calculate sym([1, 3], [3, 4, 5]), you get the concatenated array [1, 3, 3, 4, 5]. You remove the first occurance of 3 while iterating over the array, effectively skipping the second 3 that comes next :slight_smile:
    That’s why you should never modify objects you iterate over, work with a copy instead.

  2. You don’t handle the case where one array contains duplicates. The result should be a proper set meaning it should only contain unique values, i. e. [1, 2, 3] instead of [1, 1, 2, 3].


Thank you so much, now it makes sense.

Hi there,

I wrapped your code in spoiler tags. It’s unlikely that anyone looking at this topic wouldn’t already have completed the task, but it’s good practice to blur challenge solutions anyways.

Hi just wondering have you got this sorted or do you still need some help

Yeah I finished the algorithm, all I did was modify the removeFromArray() helper function and filter the end result of the main function so there are no duplicates. Thanks again.

How do I do that, please?

// your code here (wrapped in backticks)