Skip to content

Instantly share code, notes, and snippets.

@berdosi
Created May 23, 2018 15:49
Show Gist options
  • Save berdosi/6b7abc1fdc3e8bdcab0df0c5b794cde8 to your computer and use it in GitHub Desktop.
Save berdosi/6b7abc1fdc3e8bdcab0df0c5b794cde8 to your computer and use it in GitHub Desktop.
Remove duplicates from an array (to be used in Array.reduce()). If array.length may be 1, an emty array needs to be supplied as an initial value.
function makeUnique(prev, current, index) {
return (index === 1
? [prev].concat(prev !== current ? current : []) // handle when first two items are identical
: (prev.indexOf(current) > -1
? prev
: (prev.push(current), prev)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment