Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sisterAn opened this issue Nov 9, 2020 · 7 comments
Open
Labels

Comments

@sisterAn
Copy link
Owner

sisterAn commented Nov 9, 2020

No description provided.

@sisterAn sisterAn added the 华为 label Nov 9, 2020
@ohion
Copy link

ohion commented Nov 10, 2020

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]
}

@sisterAn
Copy link
Owner Author

再补充一种:使用正则

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"}

@mingju0421
Copy link

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))

@cutie6
Copy link

cutie6 commented Nov 13, 2020

再补充一种:使用正则

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"}

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

@laiyingzeng
Copy link

laiyingzeng commented Jul 6, 2021

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
Copy link

NoBey commented Mar 23, 2022

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 }), 
                    {})
          )  
}

@AlexZhang11
Copy link

AlexZhang11 commented May 24, 2024

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'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants