首页 > 试题广场 >

找出字符串中出现最多的字符和个数

[编程题]找出字符串中出现最多的字符和个数
  • 热度指数:621 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
找出字符串中出现最多的字符和个数

输入描述:
一个字符串


输出描述:
输出该字符串中出现最多的字符,以及字符出现的个数。
示例1

输入

'success'

输出

s 3
function getStr(str) {
    const s = str.trim().split('');
    const a = {};
    let obj = {text: '', value: 0}
    s.forEach(item => {
        if (!a[item]){
             a[item] = 1;
        } else {
            a[item]++;
        }
        if (a[item] > obj.value) {
            obj = {text:item,value:a[item]}
        }
    })
    console.log(obj.value, obj)
}

发表于 2021-04-26 16:57:01 回复(0)
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.next();
        char[] chars = str.toCharArray();
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        for (int i = 0; i < chars.length; i++) {
            if(map.containsKey(chars[i])){

                map.put(chars[i],map.get(chars[i])+1);

            }else {
                map.put(chars[i],1);

            }
        }
        char maxchar = 'a';
        int maxcount = 0;
        for (Character key : map.keySet()) {
            
            if(map.get(key) > maxcount){
                maxcount = map.get(key);
                maxchar = key;
            }
        }

        System.out.println(maxchar+" "+maxcount);
    }
}
//hhhhufhweuihfwehuhhhhhh

发表于 2021-06-04 10:44:43 回复(0)
function maxStr(s){
    var arr = s.split('')
    var newarr = [] 
    arr.forEach(i => {
        const Reg = new RegExp(i, 'g')
        var n = s.match(Reg)
        if(n.length > newarr.length){
            newarr = n
        }
    })
    console.log(newarr[0], newarr.length)
}
发表于 2021-05-07 17:08:30 回复(0)
str = input('')
cnt = {}
for c in str:
    if c not in cnt:
        cnt[c] = 1
    else:
        cnt[c] += 1

for item in sorted(cnt.items(), key = lambda x:x[1], reverse = True):
    print(item[0], item[1])
    break
发表于 2022-10-24 09:23:29 回复(0)
function maxCountStr(str) {
  const map = Object.create(null);
  let count = 0,
    target = '';

  for (let i = 0; i < str.length; i++) {
    map[str[i]] == undefined ? (map[str[i]] = 1) : map[str[i]]++;
    if (map[str[i]] > count) {
      count = map[str[i]];
      target = str[i];
    }
  }
  console.log(target,count)
}

发表于 2022-06-12 10:49:50 回复(0)
let m=newMap()
//n为输入的数
      for(let i of n){
          if(m.has(i)){
              m.set(i,m.get(i)+1)
          }else{
              m.set(i,1)
          }
      }
      let max=0,res
      m.forEach((item,key)=>{
          if(item>max){
              max=item
              res=key
          }
      })
      return res
发表于 2022-03-23 15:08:06 回复(0)
#es6版本

const countChar = (s) => {
    let max = null;
    let num = 1;
    constcalObj = [...s].reduce((pre, cur) => {
        pre[cur] ? pre[cur]++ : pre[cur] = 1;
        if(pre[cur] > num) {
            num = pre[cur];
            max = cur;
        }
        returnpre;
    }, {});
 
    return{ max, num };
};
编辑于 2021-11-23 17:49:02 回复(0)
const readline=require("readline");
const rl=readline.createInterface({
   input:process.stdin,
   output:process.stdout 
});
let input='';
rl.on('line',line=>{
    input=line||'';
    const res=getMaxChar(input);
    console.log(res.join(' '));
})

function getMaxChar(str){
    str=str||"";
    const n=str.length;
    const map=new Map();
    let maxChar='',maxNum=0;
    
    for(let i=0;i<n;i++){
        const num=(map.get(str[i])||0)+1;
        map.set(str[i],num);
        if(maxNum<num){
            maxNum=num;
            maxChar=str[i];
        }
    }
    return [maxChar,maxNum];
}

发表于 2021-05-07 21:17:48 回复(0)
var str = "nininihaoa";
 var o = {};
 for (var i = 0length = str.lengthi < lengthi++) {
  var char = str.charAt(i);
  if (o[char]) {
   o[char]++; //次数加1
  } else {
   o[char] = 1//若第一次出现,次数记为1
  }
 }
 console.log(o); //输出的是完整的对象,记录着每一个字符及其出现的次数
 //遍历对象,找到出现次数最多的字符的次数
 var max = 0;
 for (var key in o) {
  if (max < o[key]) {
   max = o[key]; //max始终储存次数最大的那个
  }
 }
 for (var key in o) {
  if (o[key] == max) {
   //console.log(key);
   console.log("最多的字符是" + key);
   console.log("出现的次数是" + max);
  }
 }
发表于 2021-04-25 17:03:34 回复(0)