0

I need to find the number of items in two arrays which are not duplicate.

Eg here the answer is 1:

const array1 = ['a', 'b', 'c', 'c']
const array2 = ['a', 'b', 'c']

Eg here the answer is 2:

const array1 = ['a', 'b']
const array2 = ['a', 'x', 'y']

My solution works for some inputs but not all:

function something(a, b) {

    let aArray = a.split("");
    console.log(aArray);
    let bArray = b.split("");
    console.log(bArray);

    aArray.forEach((aItem, aIndex)=> {
        console.log(aItem)
        bArray.forEach((bItem, bIndex)=> {
            console.log(bItem);
            if(aItem === bItem) {
                console.log(aIndex, bIndex);
                aArray[aIndex] = "-";
                bArray[bIndex] = "-";
            }
        });

    });

    console.log(aArray);
    console.log(bArray);

    const aRes = aArray.filter(item=> item !== "-").length;
    console.log(aRes)

    const bRes = bArray.filter(item=> item !== "-").length;
    console.log(bRes)

    const res = aRes + bRes;
    console.log(res)

     return res;
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Evanss
  • 23,390
  • 94
  • 282
  • 505

1 Answers1

-1

Stealing from this: https://stackoverflow.com/a/33034768/4903754

let difference = arr1
             .filter(x => !arr2.includes(x))
             .concat(arr2.filter(x => !arr1.includes(x)));
Isitar
  • 1,286
  • 12
  • 29