0
x = [1, 2,3, 5]; y = [1, [2], [3, [[4]]],[5,6]]));

I have to find the difference between these 2 arrays.

function arr_diff (a1, a2) {

    var a = [], diff = [];

    for (var i = 0; i < a1.length; i++) {
        a[a1[i]] = true;
    }

    for (var i = 0; i < a2.length; i++) {
        if (a[a2[i]]) {
            delete a[a2[i]];
        } else {
            a[a2[i]] = true;
        }
    }

    for (var k in a) {
        diff.push(k);
    }

    return diff;
};

This is what I tried but since it has array of arrays, this not working for me. Can anyone please suggest help.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
MMR
  • 2,869
  • 13
  • 56
  • 110
  • What outcome are you expecting? I want to know, are you identifying `2` with `[2]`, etc. – arbuthnott Sep 26 '17 at 14:53
  • What exactly do you mean by "find the differences"? Because these two arrays ave obviously _very_ different. The second one includes arrays of arrays. What's the expected output of your function? is it `[4, 6]`? Something else? – Jeremy Thille Sep 26 '17 at 14:54
  • I am expecting the diff as example ['1','2'] and ['3','1'] o/p is 2 – MMR Sep 26 '17 at 14:54
  • What, how would you get `['1','2']` and `['3','1']`? Firstly, those are strings, and not what you pass in, secondly, how is that the difference, and not `[4,6]` ? – adeneo Sep 26 '17 at 14:56
  • yes @adeneo but i seen some where in the net like the expected o/p should be ["1", "2", "3", "4", "5", "6"] – MMR Sep 26 '17 at 14:58
  • Why don't you just use your words and explain it? If you pass in the arrays `[1,2,3]` and `[1,2]`, what would the "difference" be, and why ? – adeneo Sep 26 '17 at 14:59
  • Its 3 because its because its unique. – MMR Sep 26 '17 at 15:01
  • Then, when you pass in `[1,2,3]` and `[1,[2]]`, do you still just want `3`, or `[2,3]` – adeneo Sep 26 '17 at 15:07
  • Actually I had doubt in this,whether the o/p is 3 or [2,3],can u help diciding the o/p according to u. – MMR Sep 26 '17 at 15:12

2 Answers2

0

You could just flatten the arrays before you find the difference

function arr_diff(a1, a2) {
  a1 = a1.toString().split(',');
  a2 = a2.toString().split(',');
 
  if (a1.length < a2.length) {var t = a1; a1 = a2; a2 = t};
  return a1.filter( x => (!a2.includes(x)) ).map(Number);
}

var x = [1, 2,3, 5]; 
var y = [1, [2], [3, [[4]]],[5,6]];

var r = arr_diff(x,y);

console.log(r);
adeneo
  • 312,895
  • 29
  • 395
  • 388
0

You definitely need to flatten all the arrays you want to compare using a function found here. After flattening them, you can compare them using a function found here

let x = [1, 2,3, 5];
let y = [1, [2], [3, [[4]], [5,6] ]]

const flatten = list => list.reduce(
    (a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []
)

const difference = (arr1,arr2) => {
 arr1 = flatten(arr1)
 arr2 = flatten(arr2)
 
 return arr1.filter(e => arr2.indexOf(e) === -1)
               .concat(arr2.filter(e => arr1.indexOf(e) === -1))
}

console.log(difference(x,y)) // [4,6]
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63