Created
August 1, 2016 22:57
-
-
Save chris--young/fa24f4017546a340d8140c2c80c5c846 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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