JSFiddle - React, Tailwind, and code Playground

JavaScript

let countA = 4; let countB = 5;


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

  function checkAndUpdateCount(oldVal, newBVal) {
    console.log('checkAndUpdateCount called ==========>');
    const difference = arrayDiff(oldVal, newBVal);
    console.log(difference);
    difference.forEach(item => {
      if (oldVal.includes(item) && newBVal.includes(item)) {
        console.log('no Changes');
      } else if (!oldVal.includes(item) && newBVal.includes(item)) {
        console.log(countA, countB);
        if (item === 'a') {
          countA++;
        } else {
          countB++;
        }
        console.log(countA, countB);
      } else if (oldVal.includes(item) && !newBVal.includes(item)) {
        console.log(countA, countB);
        if (item === 'a') {
          countA--;
        } else {
          countB--;
        }
        console.log(countA, countB);
      }
    });
  }

  const old1 = ['a']; const new1 = ['a', 'b'];
  const old2 = ['a']; const new2 = ['b'];
  const old3 = ['a', 'b']; const new3 = ['b'];
  const old4 = ['b', 'a']; const new4 = ['a'];
  const old5 = ['b', 'a']; const new5 = [];
  const old6 = []; const new6 = ['b', 'a'];

  checkAndUpdateCount(old1, new1); // this will return 4 6
  countA = 4; countB = 5;
  checkAndUpdateCount(old2, new2); // 4 6
  countA = 4; countB = 5;
  checkAndUpdateCount(old3, new3); // 3 5
  countA = 4; countB = 5;
  checkAndUpdateCount(old4, new4); // 4 4
  countA = 4; countB = 5;
  checkAndUpdateCount(old5, new5); // 3 4
  countA = 4; countB = 5;
  checkAndUpdateCount(old6, new6); // 2 6