Last active
February 12, 2018 10:16
-
-
Save dondevi/d0af86069837a90ea2f86b68fd21e393 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* 查找出现频率最高项 | |
* @param {Array} array | |
*/ | |
function findMostFreq (array) { | |
let freqs = {}; | |
let length = array.length; | |
let result = { item: undefined, freq: 0 }; | |
for (let i = 0; i < length; i++) { | |
let item = array[i]; | |
let freq = (freqs[item] || 0) + 1; | |
freqs[item] = freq; | |
if (freq > result.freq) { | |
result = { item, freq }; | |
} | |
} | |
return result; | |
} |
This file contains hidden or 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
let array = [0,1,2,3,4,5,6,0,6,2,3,0,6,2,0]; | |
console.log(findMostFreq(array)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment