在JavaScript中查找第二频繁出现的字符
我们需要编写一个JavaScript函数,该函数接受一个字符串并返回在字符串中出现第二多的字符。
因此,让我们为该函数编写代码-
示例
为此的代码将是-
const str = 'Hello world, I have never seen such a beautiful weather in the world';
const secondFrequent = str => {
const map = {};
for(let i = 0; i < str.length; i++){
map[str[i]] = (map[str[i]] || 0) + 1;
};
const freqArr = Object.keys(map).map(el => [el, map[el]]);
freqArr.sort((a, b) => b[1] - a[1]);
return freqArr[1][0];
};
console.log(secondFrequent(str));输出结果
控制台中的输出将为-
e
热门推荐
7 入学校简短祝福语
10 简短有力的结婚祝福语
11 宝宝100日简短祝福语
12 宝宝祝福语诗意简短 古文
13 高考给舍友祝福语简短
14 给女儿祝福语简短精辟
15 母亲说生日祝福语 简短
16 人民子弟春节祝福语简短
17 父亲简短结婚祝福语大全
18 幼儿节日祝福语大全简短