Comparing arrays of strings

If I have two arrays of words, how can I easily find the number of matches?

List 1           List 2
Cat              Dog
Dog             Cow
Cow            Lion
Puppy         Zebra
Lion            Elephant

The sequence is a different test I can do. In this example, there are three matches.

How can I easily code this?

newbiewebSr. Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kaufmed 👽Commented:
Can you use Linq?
Miguel OzSoftware EngineerCommented:
If l1 and l2 contains your words,
then the attached code will compare l1 against l2
public int GetNumberMatches(List<string> l1, List<string> l2)
            int matches = 0;
            if (l1.Count == 0 || l2.Count == 0)
                return matches;
            foreach (var item in l2)
                if (l1.Contains(item))
            return matches;

Open in new window

I can imagine an easy regex solution as well, but I dont really see the point. This is probably more effective.

Linq would also be a good choice.
List<string> list1 = new List<string>() { "Cat", "Dog", "Cow", "Puppy", "Lion" };
        List<string> list2 = new List<string>() { "Dog", "Cow", "Lion", "Zebra", "Elephant" };

        Int32 matches = 0;

        foreach (string s1 in list1)
            if (list2.Contains(s1)) matches += 1;

Open in new window

Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

       Dim aList() As String
        Dim bList() As String
        Dim Matches As Integer = 0
        For Each A As String In aList
            For Each B As String In bList
                If (A.ToUpper() = B.ToUpper()) Then  'ignore case
                    Matches = Matches + 1
                End If
string[] aList;
string[] bList;
int Matches = 0;
foreach (string A in aList) {
    foreach (string B in bList) {
        if ((A.ToUpper() == B.ToUpper())) {
            // ignore case
            Matches = (Matches + 1);
newbiewebSr. Software EngineerAuthor Commented:
Yes, I can use Linq. The other code seems good but brute force.  What can you suggest wth Linq?
kaufmed 👽Commented:
Here's a possible Linq version:

string[] List1 = { "Cat", "Dog", "Cow", "Puppy", "Lion" };
string[] List2 = { "Dog", "Cow", "Lion", "Zebra", "Elephant" };

var matchCount = (from l1 in List1
                 join l2 in List2
                 on l1 equals l2
                 select l1).Count();

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kaufmed 👽Commented:
If the lists can respectively contain duplicates, then you can call Distinct within the query:

var matchCount = (from l1 in List1.Distinct()
                  join l2 in List2.Distinct()
                  on l1 equals l2
                  select l1).Count();

Open in new window

newbiewebSr. Software EngineerAuthor Commented:
Nice code. Thanks,
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.