sisterAn/JavaScript-Algorithms

一个字符串里出现最多的字符是什么,以及出现次数

sisterAn opened this issue · 7 comments

一个字符串里出现最多的字符是什么,以及出现次数
ohion commented
function mostCharInStr(str){
    let obj = {}
    let arr = str.split('');
    let max = 1;
    let s = arr[0]
    for(let i=1;i<arr.length;i++){
        if(obj[arr[i]]){
            let count = ++obj[arr[i]]
            if(count > max){
                max = count
                s = arr[i]
            }
        }else{
            obj[arr[i]] = 1
        }
    }
    return [s,max]
}

再补充一种:使用正则

const mostCharInStr = (str) => {
    let temp = str.split('').sort().join('')
    // 先进行排序处理,然后重新组装成字符串
    let reg = /(\w)\1+/g
    let num = 0
    let value = null
    temp.replace(reg, function($0, $1){
        if (num < $0.length) {
            num = $0.length
            value = $1
        };
    });
    return {num, value}
}

// 测试
let str = 'dsfshkgfareasfd'
console.log(mostCharInStr(str))
// {num: 3, value: "f"}
let str = 'Once you learn to quit, it becomes a habit.'
const 出现最多的字符以及次数 = str => {
    let 记录出现次数最多的字符 = str[0], 字符映射 = {}
    for (let s of str) {
        if (s == ' ') continue
        s = s.toLowerCase()
        字符映射[s] = 字符映射[s] ? 字符映射[s] + 1 : 1
        记录出现次数最多的字符 = 字符映射[记录出现次数最多的字符] > 字符映射[s] ? 记录出现次数最多的字符 : s
    }
    return [记录出现次数最多的字符, 字符映射[记录出现次数最多的字符]]
}
console.log(出现最多的字符以及次数(str))

再补充一种:使用正则

const mostCharInStr = (str) => {
    let temp = str.split('').sort().join('')
    // 先进行排序处理,然后重新组装成字符串
    let reg = /(\w)\1+/g
    let num = 0
    let value = null
    temp.replace(reg, function($0, $1){
        if (num < $0.length) {
            num = $0.length
            value = $1
        };
    });
    return {num, value}
}

// 测试
let str = 'dsfshkgfareasfd'
console.log(mostCharInStr(str))
// {num: 3, value: "f"}

需要对正则的数字引用非常熟悉才能想到这种解法吧

function mostCharInStr(str){
    let obj = {}
    let arr = str.split('');
    let max = 1;
    let s = arr[0]
    for(let i=1;i<arr.length;i++){
        if(obj[arr[i]]){
            let count = ++obj[arr[i]]
            if(count > max){
                max = count
                s = arr[i]
            }
        }else{
            obj[arr[i]] = 1
        }
    }
    return [s,max]
}

for 循环 i 应该从 0 开始

NoBey commented
function mostCharInStr(str){
  return ((obj, k = Object.keys(obj).sort((a, b) => obj[b] - obj[a])[0]) => [k, obj[k]]
         )(str
            .split('')
            .reduce((obj, k) => ({...obj, [k]: (obj[k]||0)+1 }), 
                    {})
          )  
}
function getMostChar(str){
    let i = 0
    let obj = {}
    while(i<str.length){
        let c = str.charAt(i)
        if(obj.hasOwnProperty(c)){
            obj[c]+=1
        }else{
            obj[c]=1
        }
        i++
    }
    let maxValue = 0
    let res = {}
    for(let key in obj){
        if(obj[key]>maxValue){
            res={}
            maxValue = obj[key]
            res[key] = obj[key]
        }else{
            if(obj[key]==maxValue){
                res[key] = obj[key]
            }
        }
    }
    return res
}

console.log(getMostChar('aaansbsdddksda'))