题解 | #字符统计#
字符统计
https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
object 和 array 转换中 reduce()及sort()方法的使用 完整代码如下:
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void async function () { // Write your code here while(line = await readline()){ let object = line.split('').reduce((a, b) => ({...a, [b]: a[b] ? a[b] + 1 : 1}), {}) let ordered = Object.entries(object).sort(([k,a], [v,b]) => { if (a == b) { return k.charCodeAt(0) - v.charCodeAt(0) } else { return b-a } }).reduce((a, b) => a + b[0], '') console.log(ordered); } }()