Q:

Write a JavaScript program to find every element that exists in any of the two given arrays once, using a provided comparator function

0

Write a JavaScript program to find every element that exists in any of the two given arrays once, using a provided comparator function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

const union_With = (a, b, comp) =>
 Array.from(new Set([...a, ...b.filter(x => a.findIndex(y => comp(x, y)) === -1)]));

console.log(union_With([1, 1.2, 1.5, 3, 0], [1.9, 3, 0, 3.9], (a, b) => Math.round(a) === Math.round(b)));
console.log(union_With([1, 2, 3, 4], [1, 2, 3, 4, 5], (a, b) => Math.round(a) === Math.round(b)));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now