How to Calculate Number of Chars Common to Two Strings

How to calculate number of chars common to two strings?

This passes all your described test cases:

class String
def intersection(other)
str = self.dup
other.split(//).inject(0) do |sum, char|
sum += 1 if str.sub!(char,'')

How to count common characters in two strings in JavaScript?

We can convert the second input string to an array, then the next step is to iterate over the first input string and find a match in the second input string's character array.

If a match is found, increment the counter and remove that character from the second input string's character array so that it is not considered in the next match:

//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"));

Occurence of characters in common in two strings

Pretty straightforward:

count = 0

for letter in set(string1):
count += string2.count(letter)


How to identify the number of common character between two strings?

public int count(String str1, String str2) {
int result = 0;
if (str1.length() == 0 || str2.length() == 0)
return result;
if (str1.length() > str2.length()) {
for (int i = 0; i < str2.length(); i++) {
if (str2.charAt(i) == str1.charAt(i))
else {
for (int i = 0; i < str1.length(); i++) {
if (str1.charAt(i) == str2.charAt(i))
return result;

Common Character Count in Strings JavaScript

There are other more efficient answers, but this answer is easier to understand. This loops through the first string, and checks if the second string contains that value. If it does, count increases and that element from s2 is removed to prevent duplicates.

function commonCharacterCount(s1, s2) {    var count = 0;    s1 = s1.split('');    s2 = s2.split('');        s1.forEach(e => {      if (s2.includes(e)) {        count++;        s2.splice(s2.indexOf(e), 1);      }    });            return count;}
console.log(commonCharacterCount("aabcc", "adcaa"));

Count Common Characters in Strings Python

If you want to maintain a count of the number of characters in common, you should use collections.Counter instead of set.

from collections import Counter

s2 = 'qsrqq'
s1 = 'qqtrr'

common_letters = Counter(s1) & Counter(s2) # => {'q': 2, 'r': 1}
print(sum(common_letters.values())) # => 3

Count of similar characters without repetition, in two strings

You should have 2 Arrays to keep a count of the number of occurrences of each aplhabet.

int arrayCount1[26],arrayCount2[26];

Loop through strings and store the occurrences.

Now for counting the similar number of characters use:

for( int i = 0 ; i < 26 ; i++ ){
similarCharacters = similarCharacters + min( arrayCount1[26], arrayCount2[26] )

How do I get the amount of identical characters in two strings?

See Anthony's answer.

Following answer doesn't take into consideration repeated characters and hence gives wrong output if there are any repeated characters.

Iterate on first string using for...of and use includes() method to check if current character is present in second string.