-1

There are many examples on StackOverflow, but I couldn't find any that handled duplicate values in the way I need.

Given

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

I want the result to be

result = returnAllElementsFromAThatAreNotInB(a,b)
//result = [a]

result = returnAllElementsFromAThatAreNotInB(b,a)
// result = []

or let’s say I have:

a2 = [1, 2, 2, 3, 4, 4]
b2 = [1, 2, 2, 3, 3, 4]

I want to get:

result = returnAllElementsFromAThatAreNotInB(a2,b2)
//console.log(result) = [4]

result = returnAllElementsFromAThatAreNotInB(b2,a2)
//console.log(result) = [3]

the difference and symmetrical difference shown here How to get the difference between two arrays in JavaScript?

don't work. They empty arrays because it's checking only values, not instances of elements... the problem is I care about the number of elements of a value, not just the value.

ChatGPT
  • 5,334
  • 12
  • 50
  • 69
  • no sir, tired it. returned empty arrays because it's checking only values, not instances of elements... the problem is I care about the number of elements of a value, not just the value. – ChatGPT Sep 01 '22 at 14:37
  • How are you going about this? It's hard to answer questions without any actual code. – mykaf Sep 01 '22 at 14:38
  • Why does first example returns empty `filtered_b` but in second example `b_2` has difference? – Jax-p Sep 01 '22 at 14:43
  • 1
    I still don't understand it. Why `(a,b)` returns difference but `(b,a)` doesn't? What's the logic behind it? – Jax-p Sep 01 '22 at 14:49
  • @Jax-p it's not a difference per se, more a "minus": _what's left over in `a` when you remove all the elements from it that exist in `b`_. – robertklep Sep 01 '22 at 14:51
  • edited again. function should return all elements in a that are not in b – ChatGPT Sep 01 '22 at 14:52
  • yeah, I want the absolute difference, not difference in represented values... – ChatGPT Sep 01 '22 at 14:52
  • 1
    Why is your fourth result empty? Shouldn't it be `[3]`? – WillD Sep 01 '22 at 15:05
  • 1
    yes,sorry! you are correct. should be [3] (edited question to fix) – ChatGPT Sep 01 '22 at 15:14

3 Answers3

0
const uniqueDifference = (added, removed) => {
  if (added.length === 0) return [[], removed]

  const [x, ...xs] = added
  const idx = removed.findIndex(y => x === y)

  if (idx > -1) {
    return uniqueDifference(xs, removed.filter((_, i) => i !== idx))
  }

  const [as, rs] = uniqueDifference(xs, removed)
  return [[x, ...as], rs]
}
Philip Seyfi
  • 929
  • 1
  • 10
  • 24
0

You could try to loop through b and apply indexOf with splice on each element.
Minimal working code would look like this:

const diff = (a, b) => {
    //copy array to prevent mutations of inital array
    const temp = [...a]; 
    b.forEach(el => {
        let idx = temp.indexOf(el); //find element in a
        if (idx != -1) temp.splice(idx, 1); //if element found delete them
    });
    return temp;
};
const a = ["a", "a", "b"];
const b = ["a", "b"];
console.log(diff(a,b));

const a2 = [1, 2, 2, 3, 4, 4];
const b2 = [1, 2, 2, 3, 3, 4];

console.log(diff(a2,b2));
Jaood_xD
  • 808
  • 2
  • 4
  • 16
0

This will do it for you.

See Array.splice(), spread syntax (...), and Array.findIndex()

const a = ["a", "a", "b"];
const b = ["a", "b"];

const a2 = [1, 2, 2, 3, 4, 4]
const b2 = [1, 2, 2, 3, 3, 4]



console.log(returnAllElementsFromAThatAreNotInB(a, b));
// [a]

console.log(returnAllElementsFromAThatAreNotInB(b, a));
// []

console.log(returnAllElementsFromAThatAreNotInB(a2,b2));
// [4]

console.log(returnAllElementsFromAThatAreNotInB(b2,a2));
// [3]

function returnAllElementsFromAThatAreNotInB(a, b) {
  const tempA = [...a];
  b.forEach((item) => {
    const matchIndex = tempA.findIndex((tempItem) => tempItem === item);
    if (matchIndex > -1) {
      tempA.splice(matchIndex, 1);
    }
  });

  return tempA
}
WillD
  • 5,170
  • 6
  • 27
  • 56