-
Notifications
You must be signed in to change notification settings - Fork 5
unique
Subhajit Sahu edited this page May 3, 2023
·
25 revisions
Remove duplicate values.
function unique(x, fc, fm)
// x: an 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, 2, 3];
xarray.unique(x);
// → [ 1, 2, 3, 4 ]
var x = [1, 2, 3, 4, -2, -3];
xarray.unique(x, (a, b) => Math.abs(a) - Math.abs(b));
// → [ 1, 2, 3, 4 ]
xarray.unique(x, null, v => Math.abs(v));
// → [ 1, 2, 3, 4 ]