Skip to content

Instantly share code, notes, and snippets.

@bitfishxyz
Created February 17, 2020 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitfishxyz/27662b2e2d84e11460bb346db4a6066b to your computer and use it in GitHub Desktop.
Save bitfishxyz/27662b2e2d84e11460bb346db4a6066b to your computer and use it in GitHub Desktop.
function countFrequency(arr) {
return arr.reduce(function(result, ele){
// Judge whether this element has been counted before
if (result.get(ele) != undefined) {
/**
* If this element has been counted before,
* increase the frequency of its occurrence by 1
*/
result.set(ele, result.get(ele) + 1)
} else {
/**
* If this element has not been counted before,
* set the frequency of its occurrence to 1
*/
result.set(ele, 1);
}
return result;
}, new Map());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment