code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

02 Diff Two Arrays

/**
 * Created by Ashish Patel
 * Copyright © 2017 ashish.me
 * ashishsushilpatel@gmail.com 
 */

/**
 * Problem:
 * 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.
 */

function diffArray(arr1, arr2) {
    return arr1.concat(arr2).filter((item) => {
        return !arr1.includes(item) || !arr2.includes(item)
    });
}

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

Created 2020-03-31T14:01:50+00:00 · Edit