Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clear&Readable solution in Clear category for [old] The Most Frequent by SaintDron
"use strict";
function mostFrequent(data) {
let arr = [], maxIndex = 0, maxCount = 1;
data.forEach((v, i) => {
arr[v] = ++arr[v] || 1;
if (arr[v] > maxCount) {
maxCount++;
maxIndex = i;
}
});
return data[maxIndex];
}
var assert = require('assert');
if (!global.is_checking) {
console.log('Example:')
console.log(mostFrequent([
'a', 'b', 'c',
'a', 'b',
'a'
]))
assert.equal(mostFrequent([
'a', 'b', 'c',
'a', 'b',
'a'
]), 'a')
assert.equal(mostFrequent(['a', 'a', 'bi', 'bi', 'bi']), 'bi')
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
Feb. 28, 2018
Comments: