找出字符串中出现最多的字符和个数
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)
} 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 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)
} 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];
}