Skip to content

Instantly share code, notes, and snippets.

@ealipio
Created September 25, 2019 04:18
Show Gist options
  • Save ealipio/df81c53367defd28e3f2481e8b328091 to your computer and use it in GitHub Desktop.
Save ealipio/df81c53367defd28e3f2481e8b328091 to your computer and use it in GitHub Desktop.
get the most repeated number in an array
arr = [12, 10, 8, 12, 7, 6, 4, 10, 12];
const unique = [...new Set(arr)]
let obj = {};
for(let i=0;i<arr.length;i++) {
obj[arr[i]] = (obj[arr[i]] || 0 )+1
}
const max = Math.max(...Object.values(obj))
const [highFreq] = unique.filter(i => obj[i] === max)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment