题解 | #字符统计#
字符统计
https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
function sortArr(str){
// 1、转成数组并计算各个字母出现的次数
let obj = {}
str.split('').forEach(v=>{
if(obj[v]){
obj[v]++
}else{
obj[v] = 1
}
})
// 2、字母排序
return Object.entries(obj).sort((a,b)=>{
if(a[1] == b[1]){
return a[0].charCodeAt() - b[0].charCodeAt()
}else{
return b[1] - a[1]
}
}).reduce((str,cur)=>{
return str += cur[0]
},'')
}
console.log(sortArr(readline()))
// 1、转成数组并计算各个字母出现的次数
let obj = {}
str.split('').forEach(v=>{
if(obj[v]){
obj[v]++
}else{
obj[v] = 1
}
})
// 2、字母排序
return Object.entries(obj).sort((a,b)=>{
if(a[1] == b[1]){
return a[0].charCodeAt() - b[0].charCodeAt()
}else{
return b[1] - a[1]
}
}).reduce((str,cur)=>{
return str += cur[0]
},'')
}
console.log(sortArr(readline()))