每日一练

176 阅读1分钟

题目:给出一段英文连续的英文字符窜,找出重复出现次数最多的字母,例如:输入 afjghdfraaaasdenas ,输出 a

代码:

function findMaxDuplicateChar( str ) {
    if( str.length === 1 ) return str;
    const isDuplicate = {};
    for ( let i = 0, len = str.length; i < len; i++ ) {
        const ele = str [ i ];
        if( !isDuplicate[ ele ] ) isDuplicate[ ele ] = 0;
        isDuplicate[ ele ] ++
    }
    let maxStr = null, maxStrLen = 0;

    for (const key in isDuplicate) {
        const ele = isDuplicate[ key ];
        if( ele > maxStrLen ) {
            maxStrLen = ele
            maxStr = key
        } else if( ele === maxStrLen ) {
            maxStr += `,${ key }`
        }
    }
    return maxStr
}