题解 | #删除字符串中出现次数最少的字符#

删除字符串中出现次数最少的字符

https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9

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 hashArr = new Map();
        for (const i of line) {
            if (hashArr.has(i)) {
                let n = hashArr.get(i) + 1;
                hashArr.set(i, n);
            } else hashArr.set(i, 1);
        }

        let value = hashArr.values();
        let v = value.next().value;
        let min = v;
        while (v) {
            if (v < min) min = v;
            v = value.next().value;
        }

        let str = line;
        for (const i of line) {
            if (hashArr.get(i) === min) str=str.replace(i,"");
        }
        console.log(str);
    }
})();

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务