-1

Here is the question:

Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.

And here is my code:

function diffArray(arr1, arr2) {
    var newArr = [];
    // Same, same; but different.
    for (i = 0; i < arr1.length; i++) {
        for (j = 0; j < arr2.length; j++)
            while (arr1[i] === arr2[j])
                delete arr2[j];
        newArr = arr2;
    }
    return newArr;
}

console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));

Please, tell me my mistakes.

Davide Cannizzo
  • 2,826
  • 1
  • 29
  • 31
  • 3
    What is your question?? – SLaks Dec 28 '17 at 17:05
  • You're not returning a new array as the question says, you're modifying `arr2` in place. – Barmar Dec 28 '17 at 17:08
  • 2
    What does the title have to do with the code in the question? It will create an array with `undefined` elements, not `null`. – Barmar Dec 28 '17 at 17:11
  • 1
    You have to explain what is it that you want it to look like as an end result. None of us are registered telepaths and we have no idea what you're looking for. – zfrisch Dec 28 '17 at 17:12
  • Does this answer your question? [How to get the difference between two arrays in JavaScript?](https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript) – Davide Cannizzo Aug 23 '20 at 13:09
  • Duplicate of https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript – Davide Cannizzo Aug 23 '20 at 13:10

5 Answers5

0

The task you are trying to complete asks you to create a new array, but instead you modify arr2. It would probably be easiest to just copy all elements not included in the other array to a new array, like this:

function diffArray(arr1, arr2) {
    var newArray = [];
    arr1.forEach(function(el) {
        if (!arr2.includes(el)) {
            newArray.push(el);
        }
    });
    arr2.forEach(function(el) {
        if (!arr1.includes(el)) {
            newArray.push(el);
        }
    });

    return newArray;
}

If you would rather try and fix your code instead, I can try to have another look at it.

Jotha
  • 418
  • 2
  • 7
0

I've used Array.prototype.filter method:

function diffArray(arr1, arr2) {
    var dif01 = arr1.filter(function (t) {
        return arr2.indexOf(t) === -1;
    });

    var dif02 = arr2.filter(function (t) {
        return arr1.indexOf(t) === -1;
    });

    return (dif01).concat(dif02);
}

alert(diffArray([1, 2, 3, 6, 5], [1, 2, 3, 4, 7, 5]));

If you still want to use your code and delete the common elements, try to use Array.prototype.splice method instead of delete: the latter deletes the value, but keeps the index empty, while Array.prototype.splice will remove those whole indices within the given range and will reindex the items next to the range.

Davide Cannizzo
  • 2,826
  • 1
  • 29
  • 31
Ofir G
  • 736
  • 6
  • 18
0

Try this:

function diffArray(arr1, arr2) {
    var ret = [];

    function checkElem(arrFrom, arrIn) {
        for (var i = 0; i < arrFrom.length; ++i) {
            var elem = arrFrom[i];
            if (arrIn.indexOf(elem) === -1)
                ret.push(elem);
        }
    }

    checkElem(arr1, arr2);
    checkElem(arr2, arr1);
    return ret;
}

I hope this can solve your problem.

Davide Cannizzo
  • 2,826
  • 1
  • 29
  • 31
rodrigocfd
  • 6,450
  • 6
  • 34
  • 68
0

If you work using indices as references which you delete, you'll leave those indices undefined. You have to use push to add an item and splice to remove one. The time complexity of the following code should be: O(nm) where n and m are the lengths of the arr1 and arr2 arrays respectively.

function diffArray(arr1, arr2) {
    var newArr = [];
    for (i = 0; i < arr1.length; i++) {
        for (j = 0; j < arr2.length; j++)
            while (arr1[i] === arr2[j])
                arr2.splice(j, 1);
        newArr = arr2;
    }
    return newArr;
}

console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));

This should work, but I've found a different way that is a bit slower for short arrays but much faster for longer array. The time complexity of the following code should be: O(3(n + m)), which is reduced to O(n + m) where n and m are the lengths of the arr1 and arr2 arrays respectively.
Look at this fiddle.
Here's it:

function diffArray(arr1, arr2) {
    let obj1 = {}, obj2 = {};
    for (let l = arr1.length, i = 0; i < l; i++)
        obj1[arr1[i]] = undefined;
    for (let l = arr2.length, i = 0; i < l; i++)
        obj2[arr2[i]] = undefined;
    let a = [];
    for (let arr = arr1.concat(arr2), l = arr.length, i = 0, item = arr[0]; i < l; i++, item = arr[i])
        if (item in obj1 !== item in obj2)
            a.push(item);
    return a;
}

console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));
Davide Cannizzo
  • 2,826
  • 1
  • 29
  • 31
0

You can use Array.prototype.filter:

var array1 = [1, 2, 3, 5];
var array2 = [1, 2, 3, 4, 5];

var filteredArray = filter(array1, array2).concat(filter(array2, array1));

function filter(arr1, arr2) {
    return arr1.filter(function(el) { return arr2.indexOf(el) < 0; });
}

Here is a working JSFiddle.

Davide Cannizzo
  • 2,826
  • 1
  • 29
  • 31
FabioG
  • 2,936
  • 3
  • 31
  • 50