WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

intersection

Subhajit Sahu edited this page May 3, 2023 · 26 revisions

Obtain values present in both arrays.

Similar: union, intersection, difference, symmetricDifference.
Similar: isUnique, isDisjoint, intersection.


function intersection(x, y, fc, fm)
// x:  an array
// y:  another array
// fc: compare function (a, b)
// fm: map function (v, i, x)

⏱️ Compare function ⇒ O(n²).

const xarray = require('extra-array');

var x = [1, 2, 3, 4];
var y = [2, 3, 5];
xarray.intersection(x, y);
// → [ 2, 3 ]

var y = [-2, -3, -5];
xarray.intersection(x, y, (a, b) => Math.abs(a) - Math.abs(b));
// → [ 2, 3 ]

xarray.intersection(x, y, null, v => Math.abs(v));
// → [ 2, 3 ]


References

Clone this wiki locally