I have array contains strings. I have to read the words one by one and output words which has any character in it appearing exactly two times. But my code also show 3 or more same character also. How can I output the words which only character appearing two times ? For example not showing : "aaaa" or "aaab"
const words = [
"asdf",
"fdas",
"asds",
"d fm",
"dfaa",
"aaaa",
"aabb",
"aaabb"
];
function checkString(text,index){
if((text.length - index) == 0 ){ //stop condition
return false;
}else{
return checkString(text,index + 1)
|| text.substr(0, index).indexOf(text[index])!=-1;
}
}
// example Data to test
for(var idx in words){
var txt = words[idx];
if(checkString(txt,0)) {
console.log(txt);
}
}
const words = [
"asdf",
"fdas",
"asds",
"d fm",
"dfaa",
"aaaa",
"aabb",
"aaabb"
];
/*
Output have to be :
asds
dfaa
aabb
aaabb
*/