Check number of same values in 2 strings

Hello all!
I have a question. I would like to find the number of same digits in 2 strings.
For instance :
string 1: 2678, string 2 : 9992
Return should be 1, since “2” is repeated once

string 1: 3838, string 2 : 8383
Return should be 4, since “3” is repeated twice and “8” is repeated twice.

I am currently thinking of a for loop through string 1, to see if each item is in string 2.
IE:
string 1: 3838, string 2 : 8383
count = 0

For i in string 1:
if i in string 2:
count = count +1
string 2.remove(i). (# To prevent the same digit being counted again. However, if i want to remove “3” for string 2, i only want to remove it once. Using .replace will remove it twice and I am unable to fix this)

The position of numbers do not matter, but for my project, both string contains 4 digit numbers.
I would appreciate any form of help.

Thank you and have a great day!

Hi,

How about the following expression?

string2.Count(function(x) string1.Contains(x))

Regards,

4 Likes

@Yoichi
Thank you for this elegant line of code. Work like a charm.

1 Like