Created
September 25, 2019 04:18
-
-
Save ealipio/df81c53367defd28e3f2481e8b328091 to your computer and use it in GitHub Desktop.
get the most repeated number in an array
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
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