Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris--young/fa24f4017546a340d8140c2c80c5c846 to your computer and use it in GitHub Desktop.
Save chris--young/fa24f4017546a340d8140c2c80c5c846 to your computer and use it in GitHub Desktop.
'use strict'
const a = [{ id: 1 }, { id: 2 }, { id: 1 }, { id: 3 }]; // guaranteed to have a length less than 25
function unique1(a) {
const r = [];
o: for (let o of a) {
for (let i of r)
if (o.id === i.id)
continue o;
r.push(o);
}
return r;
}
function unique2(a) {
const m = new Map();
const r = [];
for (let e of a)
m.set(e.id, e);
for (let v of m.values())
r.push(v);
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment