标签:har ber reduce char get pre match number blog
//方法1 function getMaxNumberOfChar(str) { return (str + ‘‘).split(‘‘).reduce(function(pre, cur, index, arr) { cur in pre ? pre[cur]++ : (pre[cur] = 1); pre[cur] > pre.value && (pre.char = cur, pre.value = pre[cur]); return pre; }, {value: 0}); } var result = getMaxNumberOfChar(‘ababccdeajxac‘); console.log(result);
//方法2 function getMaxNumberOfChar(str) { return (str + ‘‘).split(‘‘).sort().join(‘‘).match(/(\w)\1*/g).reduce(function(pre, cur) { return cur.length > pre.value ? {value: cur.length, char: cur[0]} : pre; }, {value: 0}) } var result = getMaxNumberOfChar(‘ababccdeajxac‘); console.log(result);
标签:har ber reduce char get pre match number blog
原文地址:http://www.cnblogs.com/camille666/p/str_most_times_letter.html