/**
* 最常出现的字母
*/
interface IMap{
[propName: string]: number;
}
const most_Commom_Occurs = (str: string) => {
let sMap:IMap = {}
let strArray = str.split('')
for(let i=0; i< strArray.length; i++){
if(!sMap[strArray[i]]){
sMap[strArray[i]] = 1
continue
}
sMap[strArray[i]] = sMap[strArray[i]] + 1
}
let max = 0
let max_str = ''
for(let i in sMap){
if(sMap[i]> max){
max = sMap[i]
max_str = i
}
}
return max_str
}
console.log(most_Commom_Occurs('tomorrow'))
字符串里面出现频率最高的字符
最新推荐文章于 2023-07-12 07:40:25 发布