diffArray

Medium · Array Manipulations · 2 test cases

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.

Examples

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]) → [4]

Starter code

function diffArray(arr1, arr2) {

}

Complexity of the optimal solution

Time O(n*m), space O(n+m). This solution uses `filter` with `includes`. For each element in arr1 (size n), it searches arr2 (size m), making it O(n*m). The same happens for the second filter. The space for the new array is O(n+m) in the worst case.

Solve diffArray in the browser