在一个 Javascript 字符串中计算不重复的字符
我们要求编写一个 JavaScript 函数,该函数接受一个字符串,并返回字符串中冗余字符的计数。
例如:如果字符串为 −
const str = 'abcde'
那么输出应该是 0。
如果字符串为 −
const str = 'aaacbfsc';
那么输出应该是 3。
示例
此代码为 −
const str = 'aaacbfsc';
const countRedundant = str => {
let count = 0;
for(let i = 0; i < str.length; i++){
if(i === str.lastIndexOf(str[i])){
continue;
};
count++;
};
return count;
};
console.log(countRedundant(str));输出
控制台中的输出将为 −
3
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP