Search code examples
javascriptarraysarray-difference

How to find difference between two same array in JS?


I want to find difference between two arrays. I tried below code from one of the solution given on SF community, but not working from me.

My below code is not working if there is number repeated EVEN NUMBER of time(like "7" repeated twice in both the arrays a & b and in result I am getting "7" as difference),

whereas code is working for number repeated ODD NUMBER of time(like "11" repeated thrice in both the arrays a & b and in result it cancel all the "11" and return nothing for this).

I want the solution to find difference or cancels the number repeated ANY NUMBER of times, I mean if number like "7" repeated twice in both the arrays then it should cancels both the number and return NOTHING.

And given two arrays are, you can find more examples like "7" and "11" in belows given arrays

a = ["1", "3", "17", "12", "11", "17", "7", "7", "85", "16", "5", "30", "11", "81", "20", "49", "8", "17", "11", "8", "13", "81"]

b = ["5", "20", "78", "13", "85", "81", "12", "17", "16", "8", "17", "3", "7", "1", "11", "9", "8", "17", "49", "30", "81", "11", "11", "7"]

Below is the code I am using to find difference between two arays,

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);
}

console.log(diff);
return diff;
}

DIFFERENCE/RESULT : I get from two arrays is : ["7", "8", "9", "78", "81", "getUnique"]

EXPECTED RESULT : from two arrays I want : ["9", "78"]

Please help, any help will be appreciated.


Solution

  • If you want to keep the extra values in the difference array too, ex. for

    a = [1,1,1,1]
    b = [1]
    

    the result: [1,1,1], I'd do this: count occurrences in first array and subtract the count from the second array.

    function getDifference(array1, array2) {
      const counter = {}
      const result = []
    
      function addNumberToCounter(number) {
        const currentCount = counter[number] || 0
        counter[number] = currentCount + 1
      }
      function subtractNumberFromCounter(number) {
        const currentCount = counter[number] || 0
        counter[number] = currentCount - 1
      }
      function addNumberToResult(number, times) {
        for(let i = 0; i < times; i++) {
          result.push(number)
        }
      }
      
      array1.forEach(addNumberToCounter)
      array2.forEach(subtractNumberFromCounter)
      
      for (let number in counter) {
        const numberCount = Math.abs(counter[number])
        addNumberToResult(number, numberCount)
      }
      
      return result
    }
    
    const a = ["1", "3", "17", "12", "11", "17", "7", "7", "85", "16", "5", "30", "11", "81", "20", "49", "8", "17", "11", "8", "13", "81"]
    const b = ["5", "20", "78", "13", "85", "81", "12", "17", "16", "8", "17", "3", "7", "1", "11", "9", "8", "17", "49", "30", "81", "11", "11", "7"]
    const result = getDifference(a, b)
    console.log(result)