题解 | #删除字符串中出现次数最少的字符#
删除字符串中出现次数最少的字符
https://www.nowcoder.com/practice/05182d328eb848dda7fdd5e029a56da9
//获取输入流 line = readline(); //新建空白数组arr1, arr2分别存储字符串元素及出现相应次数数据 var arr1 = []; var arr2 = []; //for循环依次统计各字母出现数据 for (let i = 0; i < line.length; i++) { var index = arr1.indexOf(line[i]); if (index == -1) { arr1.push(line[i]); arr2.push(1); } else { arr2[index] += 1; } } //假定最小次数为arr2[0];;遍历arr2,找到最小次数 var result = arr2[0]; for (let j = 0; j < arr2.length; j++) { if (arr2[j] <= result) { result = arr2[j] } } //新建indexes数组,再次遍历arr2数组,如数组中有其余相同最小次数,将其在arr2中的排位序列(对应arr1中字母的排位)push进indexes数组 var indexes = []; for (let k = 0; k < arr2.length; k++) { if (arr2[k] == result) { indexes.push(k); } } //利用for循环及正则表达式,依次将最小次数字母替换成空白格 for (let l = 0; l < indexes.length; l++) { var re = new RegExp(arr1[indexes[l]], 'g'); line = line.replace(re, ""); } //显示结果 console.log(line);