1

I have some working code, though it seems like it should be much more efficient. I feel there are too many filters working through the same data sets. Is there a cleaner or more efficient way of merging 2 arrays without duplicating?

const x = [1,3,7,4,9];
const y = [2,3,9,13,4];

const yFilteredByX = y.filter(element => x.includes(element));
const xFilteredByY = x.filter(element => y.includes(element));

const unique = (value, index, self) => {
    return self.indexOf(value) === index;
};

const newArr = yFilteredByX.concat(xFilteredByY);
const uniqueArr = newArr.filter(unique);

console.log(uniqueArr);

Currently outputs [3,9,4] successfully.

I made a quick fiddle

matsev
  • 32,104
  • 16
  • 121
  • 156
  • why do you do not use just a single filter? if an item is in the other array you get the wanted result set. – Nina Scholz Jul 02 '22 at 17:21
  • Thank you, I noticed that oversight after posting - you are correct. I was thinking about if the second set didn't have a value in the first set, it wouldn't be considered. But since I'm looking for values in BOTH, this would not be a problem. – Ryan Froese Jul 05 '22 at 06:39

3 Answers3

3
  • Create a Set for each array
  • Iterate over the items of the first using Array#filter and return only those in the second set

const _getCommonElements = (arr1 = [], arr2 = []) => {
  const set1 = new Set(arr1), set2 = new Set(arr2);
  return [...set1].filter(e => set2.has(e));
}

console.log( _getCommonElements([1,3,7,4,9], [2,3,9,13,4]) );
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
1

It seems like you intend to calculate the intersection of two sets, thus the corresponding function operation from the mozilla developer docs can be used, e.g.

const x = [1, 3, 7, 4, 9];
const y = [2, 3, 9, 13, 4];

function intersection(setA, setB) {
  let _intersection = new Set()
  for (let elem of setB) {
    if (setA.has(elem)) {
      _intersection.add(elem)
    }
  }
  return _intersection
}

const setX = new Set(x);
const setY = new Set(y);

const commonSet = intersection(setX, setY);

console.log(...commonSet); // [3, 9, 4]
matsev
  • 32,104
  • 16
  • 121
  • 156
  • Wow, didn't know intersection was a JS function - thank you! Is this more efficient than the array/filter method? I'm curious what's going on "under the hood" of this function. – Ryan Froese Jul 05 '22 at 06:43
  • btw, the canonical use of a set is to use it directly in a filter method, like `common = array1.filter(Set.prototype.has, new Set(array2))`. – Nina Scholz Jul 05 '22 at 06:47
  • @RyanFroese no intersection is not currently a JS function. I guess that is why the maintainers of the mozilla developer docs decided to add an example of how it can be implemented, see link above – matsev Jul 06 '22 at 08:36
  • @NinaScholz thanks for sharing. The intersection function I provided is a copy / paste of the one that is provided in the mozilla developer docs, see link above – matsev Jul 06 '22 at 08:38
0

Create a Set from one, and then filter the other by checking if the set has elements.

const x = [1, 3, 7, 4, 9];
const y = [2, 3, 9, 13, 4];

const uniqueInX = new Set(x);
const uniqueInBoth = y.filter(e => uniqueInX.has(e));

console.log(uniqueInBoth);
codemode
  • 478
  • 3
  • 11