Skip to content

Instantly share code, notes, and snippets.

@javierd79
Created September 24, 2022 21:17
Show Gist options
  • Save javierd79/58527002f23ce08fe3981f302405384a to your computer and use it in GitHub Desktop.
Save javierd79/58527002f23ce08fe3981f302405384a to your computer and use it in GitHub Desktop.
Function to count how many elements are duplicates in an Array
const countDuplicate = (array) => {
let object = array.reduce((prev, cur) => ((prev[cur] = prev[cur] + 1 || 1), prev), {})
console.log(object);
}
countDuplicate(['test', 'test', 'test2'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment