Review my code for (Diff Two Arrays)

It is different from the other solutions and somewhat easier to understand in my humble opinion —

function diffArray(arr1, arr2) {
    const newArr = [];
    for(const element of arr1) {
        if(!arr2.includes(element)) {
            newArr.push(element);
        }
    }

    for(const element of arr2) {
        if(!arr1.includes(element)) {
            newArr.push(element);
        }
    }

    return newArr;
}

Ok I think I see what you mean, like this … :thinking:

function diffArray(arr1, arr2) {
    const newArr = [];

    findDiff(arr1, arr2);
    findDiff(arr2, arr1);

    function findDiff(arrA, arrB) {
        for(const element of arrA) {    // check every element of arrA ...
            if(!arrB.includes(element)) {   // if that element not found in arrB add it to newArr
                newArr.push(element);
            }
        }
    }

    return newArr;
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.