Skip to content

Instantly share code, notes, and snippets.

@davidnagli
Created January 14, 2018 21:52
Show Gist options
  • Save davidnagli/be72fa79e4ac6d6bff3ed0af240ebf18 to your computer and use it in GitHub Desktop.
Save davidnagli/be72fa79e4ac6d6bff3ed0af240ebf18 to your computer and use it in GitHub Desktop.
cdup-js : count duplicates in a javascript array (find the count of each unique element in an array) with this simple, easy, and FAST one liner
// Readable version:
array.reduce((countsMap, item) => countsMap.set(item, countsMap.get(item) + 1 || 1), new Map)
// Minified version:
array.reduce((a,b)=>a.set(b,a.get(b)+1||1),new Map)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment