Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSS

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"));
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #find #common #characters #strings #javascript
ADD COMMENT
Topic
Name
9+4 =