const text = 'abcddefghiijkmnopqrs';
function queryTheMostLetter(text) {
const array1 = Array.from(text);
const tempes = {};
for (let i = 0, l = array1.length; i < l; i++) {
if (tempes[array1[i]]) {
tempes[array1[i]] += 1;
} else {
tempes[array1[i]] = 1;
}
}
const array2 = [];
for (const key in tempes) {
array2.push(tempes[key]);
}
// const maxes = Math.max.apply(null, array2);
const maxes = Math.max(...array2);
const objes = [];
for (const key in tempes) {
if (tempes[key] === maxes) {
objes.push(key);
}
}
return objes;
}
console.log(queryTheMostLetter(text));
查找字符串中出现次数最多的字符
最新推荐文章于 2023-12-15 18:55:24 发布
本文介绍了一种使用JavaScript编程语言的方法,用于确定给定字符串中出现频率最高的字符。通过将字符串转换为字符数组,然后利用对象来记录每个字符的出现次数,最后找出出现次数最多的字符。

1万+

被折叠的 条评论
为什么被折叠?



