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

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

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届毕业生现状#
全部评论

相关推荐

头像
02-26 13:58
门头沟学院 Java
北城_阿亮:把八股背一背,包装一下实习经历项目经历,要是有心思考证就考一考,然后把别人的项目爬到自己github上,包装到简历里,什么三个月?一个月!
点赞 评论 收藏
分享
神哥不得了:神哥来啦~自我评价和校园经历的话可以直接删了,从大厂暑期的话应该没有什么太多问题,应该是能拿到很多大厂面试机会的,就是在面试的时候表示的好一点就行,可以在面试前先把高频top 50的八股多巩固几遍,千万不要看那些假高频八股,这两个项目的话问题不是很大,应该能够帮你找到大厂实习的,算法的话一定要刷起来,因为大厂有些还是比较看重算法的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务