Skip to content

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

Open

Description

@sisterAn
No description provided.

Activity

  1. ohion commented on Nov 10, 2020

    @ohion
    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]
    }
  2. sisterAn commented on Nov 11, 2020

    @sisterAn
    OwnerAuthor

    再补充一种:使用正则

    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"}
  3. mingju0421 commented on Nov 11, 2020

    @mingju0421
    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))
  4. cutie6 commented on Nov 13, 2020

    @cutie6

    再补充一种:使用正则

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

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

  5. laiyingzeng commented on Jul 6, 2021

    @laiyingzeng
    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 开始

  6. NoBey commented on Mar 23, 2022

    @NoBey
    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 }), 
                        {})
              )  
    }
  7. AlexZhang11 commented on May 24, 2024

    @AlexZhang11
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions