This post mark down the logic using javaSrcipt get the modes, source samples are with string data type and separated by comma.
<script>
const ROUND_TO_FIXED = 2;
function _stringToList(string){
return string.split(",");
}
function _roundToFixed(num){
return Number(parseFloat(num).toFixed(ROUND_TO_FIXED));
}
function getSampleModes(string){
var sourceList = _stringToList(string);
var max = -Infinity, x;
var json = {},
frequencies = {}
var modes = [];
//Records the occurs of numbers
for(var key in sourceList){
if(! (sourceList[key] in frequencies)){
frequencies[sourceList[key]] = 1;
}else{
frequencies[sourceList[key]] += 1;
}
}
// Get modes
for(var key in frequencies) {
if( frequencies[key] > max) max = frequencies[key];
if( frequencies[key] == max) modes.push(key);
}
return modes;
}
var source = "1,2,3,5,1,2,6"
console.log(getSampleModes(source));
</script>
Result
Refercene:
https://en.wikipedia.org/wiki/Mode_(statistics)
Monday, August 12, 2019
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment