Function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string.
"abcde" -> 0 # no characters repeats more than once
"indivisibility" -> 1 # 'i' occurs six times rs twice
The problem is that each iteration loops meet on the same char and compare it. How can I avoid it?
function duplicateCount(text){
var texT = text.toLowerCase();
var count = 0;
var total = 0;
for(var i = 0; i < texT.length; i++ ){
var char = texT[i];
if(count > 1){
total = total + 1;
}
for ( var j = 0; j < texT.length; j++){
var char2 = texT[j];
if(char === char2){
count = count + 1;
}
}
}
return total;
}
duplicateCount('kBHhJkj8l8');
function duplicateCount(text){
var texT = text.toLowerCase();
const obj = {};
for(var i = 0; i < texT.length; i++ ){
if(obj[texT[i]]) {
obj[texT[i]] += 1;
}
else {
obj[texT[i]] = 1;
}
}
let total = 0;
Object.keys(obj).forEach(key => {
if(obj[key] != 1){
total += 1;
}
})
return total;
}
console.log(duplicateCount('kBHhJkj8l8'));