probably-asskicker
probably-asskicker

Reputation: 171

Difference of array, result is 1 or undefined

Array a = [1,2,3,4,5,5]

Array b = [1,2,3,5]

c = a - b

which should return c = [4] (5 is repeated twice but I dont want it in the output) Now here is my code:

function arrayDiff(a, b) {
  var array = [];
  var diff = [];
  for (var i = 0; i<a.length; i++) {
    array[a[i]] = true;
  }

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

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

  }

}
Test.describe("Sample tests", function() {
  Test.it("Should pass Sample tests", function() {
    Test.assertDeepEquals(arrayDiff([], [4,5]), [], "a was [], b was [4,5]");
    Test.assertDeepEquals(arrayDiff([3,4], [3]), [4], "a was [3,4], b was [3]");
    Test.assertDeepEquals(arrayDiff([1,8,2], []), [1,8,2], "a was [1,8,2], b was []");
  });
}); 

but it returns weird stuff. Can you please explain why it returns 1 and how do I fix it? This is the console.log output:


a was [], b was [4,5] - Expected: [], instead got: 1
a was [3,4], b was [3] - Expected: [4], instead got: 1
a was [1,8,2], b was [] - Expected: [1, 8, 2], instead got: undefined


Unhandled rejection TestError: a was [], b was [4,5] - Expected: [], instead got: 1

Can you please help me fix it?

Upvotes: 1

Views: 108

Answers (3)

Yousaf
Yousaf

Reputation: 29312

there are couple of problems in your code

  1. 3rd for loop that is nested inside the 2nd for loop should not be nested inside the 2nd loop.

  2. .push() method adds a new item in the array and returns the length of the array after adding the new item. Instead of returning the return value of push function, you need to return diff array.

Here's the fixed code

const a = [1,2,3,4,5,5];
const b = [1,2,3,5];

function arrayDiff(a, b) {
  var array = [];
  var diff = [];
  for (var i = 0; i<a.length; i++) {
    array[a[i]] = true;
  }
  
  for (var i = 0; i<b.length; i++) {
    if (array[b[i]]) {
      delete array[b[i]];
    }
    else {
      array[b[i]] = true;
    }
  }
    
  for (var k in array) {
      diff.push(Number(k));
  }
  
  return diff;
  
}

console.log(arrayDiff(a, b));

Edit

As per your comments, if a = [] and b = [1, 2] then output should be [] and for a = [1, 8, 2] and b = [], output should be [1, 8 ,2].

This isn't possible with your code as you are finding the difference based on array indexes and boolean values.

You can get the desired output by filtering the array a and checking if the current element in array a exists in array b or not.

let a = [1, 8 ,2];
let b = [];

function arrayDiff(a, b) {
  return a.filter(n => !b.includes(n));
}

console.log(arrayDiff(a, b));

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386680

You could take a Set and filter the array.

function arrayDiff(a, b) {
    const setB = new Set(b);
    return a.filter(v => !setB.has(v));
}

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

Upvotes: 0

TopWebGhost
TopWebGhost

Reputation: 335

Your code looks good. It may need some modification to make it simple.

function arrayDiff(a, b) {
  return a.filter((aItem) => b.indexOf(aItem) === -1);
}

Upvotes: 0

Related Questions