Js counts the most frequently occurring letters and digits in a string

let str = 'asdfg2hjklaqw2ert2yuiopbiaia21bbbb54562102bbbbbbbb0a124a511a22a2a242a1a';
let countRateResult = countRate(str);
console.log("countRateResult", countRateResult)
// Write the low method
function countRate(arr) {
	Split ('') returns an array of each character in the string if the parentheses are empty
	let strArray = arr.split(' ');
	// let countArr = {}; // Can also be stored as key-value
	let countArr = [];
	strArray.forEach((item, index) = >{
		// countArr[item] = 0;
		countArr[index] = 0
		for(let i=0; i<strArray.length; i++) {
			if (item == strArray[i]) {
				// countArr[item]++;countArr[index]++; }}});let countArrResultName = strArray[0];// Let the maximum value and number be the first one in the array
	let countArrResultNum = countArr[0];
	for(let i=0; i<countArr.length; i++) {
		if(countArr[i] > countArrResultNum) { countArrResultName = strArray[i]; countArrResultNum = countArr[i]; }}return [countArrResultName, countArrResultNum];
}
Copy the code

I forget which url it is. If you see a better one, make a note of it

1 / / method
let str = 'asdfghjklaqwertysddfsdewffffffadfsdfeererefdfefuiopiaia';
const strChar = str= > {
    let string = [...str],
        maxValue = ' ',
        obj = {},
		max = 0;
    string.forEach(value= > {
        obj[value] = obj[value] == undefined ? 1 : obj[value] + 1;/ / window
        if (obj[value] > max) {
            max = obj[value]
            maxValue = value
        }
	})
	console.log("obj", obj);
	return maxValue;
}
console.log(strChar(str));
2 / / method
let str = 'safaA890654uasfAJIFj32342HDWEFH3Da23AueUEWda';
let results = {};
let rs = str.split(' ');

rs.forEach(function(al){
    if(results[al] === undefined){
        results[al] = 1;
    }else{ results[al]++; }})let keys = Object.keys(results);
for(let i = 0; i < keys.length; i++){
    console.log(keys[i] + ':' + results[keys[i]]);
}
Copy the code