Skip to content

Instantly share code, notes, and snippets.

@dondevi
Last active February 12, 2018 10:16
Show Gist options
  • Save dondevi/d0af86069837a90ea2f86b68fd21e393 to your computer and use it in GitHub Desktop.
Save dondevi/d0af86069837a90ea2f86b68fd21e393 to your computer and use it in GitHub Desktop.
/**
* 查找出现频率最高项
* @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;
}
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