题解 | #合并表记录#

合并表记录

https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201

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
    let input = []
    let hash = new Map()
    while(line = await readline()){
        input.push(line)
    }
    input.shift()
    //拿到对应的数组
     input.forEach((value)=>{
        let data = value.split(' ')
        if(hash.has(data[0])){
            hash.set(data[0],Number(data[1]) + Number(hash.get(data[0])) + '')
        }else{
            hash.set(data[0],data[1])
        }
     })
    //map升序排列
    hash = new Map([...hash].sort((a,b)=>{
        return Number(a[0]) - Number(b[0])
     }))
    //输出
    for(let item of hash.entries()){
        console.log(item[0] + ' ' + item[1])
    }
}()

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务