find common characters in two strings javascript

Solutions on MaxInterview for find common characters in two strings javascript by the best coders in the world

showing results for - "find common characters in two strings javascript"
Maximilien
25 Jul 2019
1var s1 = "abcd";
2var s2 = "aad";
3var count=0;
4function match(s1,s2)
5{
6for(let i in s1)
7s2.includes(s1[i])?count++:false;
8console.log(count)
9}
10match(s1,s2)
Máximo
01 Feb 2018
1//Solution:
2function getSameCount(str1, str2) {
3  let count = 0;
4  const obj = str2.split("");
5  for(str of str1){
6    let idx = obj.findIndex(s => s === str);
7    if(idx >= 0){
8      count++;
9      obj.splice(idx, 1);
10    }
11  }
12  return count;
13}
14
15//Test:
16console.log(getSameCount("abcd", "aad"));
17console.log(getSameCount("geeksforgeeks", "platformforgeeks"));
18console.log(getSameCount("aad", "abcd"));
19console.log(getSameCount("platformforgeeks", "geeksforgeeks"));