Q:

compare two strings and count number of similar characters

I am trying to write a js function which compares two strings and return the number of similar occurrences. But my function is 
 considering duplicates as well.
 For example, two words 'state' and 'tall'. Ideally it should return a count of two but it is returning three considering two duplicate t's in 'state'. Can anyone help me out with this

A:

Here is ES5 solution:

function solution(str1, str2) {
    var count = 0;
    var find = -1;
    for (var i = 0; i < str1.length; i++) {
        find = str2.indexOf(str1.charAt(i));
        if (find > -1) {
          count++;
          str2 = str2.substr(0, find) + str2.substr(find + 1);
        }
    }
    return count;
};

console.log(solution('state', 'tall'));

