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

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

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

//1. 字符串转为数组 遍历之后用对象key存字段 value存出现次数
//2. 遍历对象 用找出value最小值 如果相等 则都存一个数组中
//3. 数组转为字符串 replace替换方法 将相同的替换为空字符串
const readline = require('readline')
const rl = readline.createInterface({
    input:process.stdin,
    output:process.stdout
});
rl.on('line',(line)=>{
    let countRecord = {}
    let inputArray = line.split('');
    for(let i = 0;i<inputArray.length;i++){
        if(!countRecord[inputArray[i]]){
            countRecord[inputArray[i]]=0;
        }
        countRecord[inputArray[i]] += 1
    }
    let target = []
    let count = 0
    for( let k in countRecord){
        if(count == 0){
            target.push(k)
            count=countRecord[k]
        }else if(countRecord[k]<count){
            target.length = 0;
            target.push(k)
            count = countRecord[k]
        }else if( countRecord[k] == count ){
            target.push(k)
        }
    }
    let regex = new RegExp(`[${target.join('')}]`,'g')
    console.log(line.replace(regex,''))
})

#2022届毕业生现状#
全部评论

相关推荐

一tiao酸菜鱼:秋招还没正式开始呢,就准备有结果了。。。。?
点赞 评论 收藏
分享
05-19 19:57
蚌埠学院 Python
2237:Gpa70不算高,建议只写排名,个人技能不在多而在精,缩到8条以内。项目留一个含金量高的,减少间距弄到一页,硕士简历也就一页,本科不要写很多
实习,投递多份简历没人回...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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