find common characters in two strings javascript code example
Example 1: find common characters in two strings javascript
var s1 = "abcd";
var s2 = "aad";
var count=0;
function match(s1,s2)
{
for(let i in s1)
s2.includes(s1[i])?count++:false;
console.log(count)
}
match(s1,s2)
Example 2: find common characters in two strings javascript
//Solution:
function getSameCount(str1, str2) {
let count = 0;
const obj = str2.split("");
for(str of str1){
let idx = obj.findIndex(s => s === str);
if(idx >= 0){
count++;
obj.splice(idx, 1);
}
}
return count;
}
//Test:
console.log(getSameCount("abcd", "aad"));
console.log(getSameCount("geeksforgeeks", "platformforgeeks"));
console.log(getSameCount("aad", "abcd"));
console.log(getSameCount("platformforgeeks", "geeksforgeeks"));