Created
February 17, 2020 08:21
-
-
Save bitfishxyz/27662b2e2d84e11460bb346db4a6066b to your computer and use it in GitHub Desktop.
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
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