题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
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届毕业生现状#