Answers for "find common characters in two strings javascript"

0

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)
Posted by: Guest on April-17-2021
0

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"));
Posted by: Guest on April-17-2021

Code answers related to "find common characters in two strings javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language