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?

Thanks,
newbieweb
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.

käµfm³d 👽Commented:
Can you use Linq?
0
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))
                    matches++;
            }
            return matches;
        }

Open in new window

0
ddayx10Commented:
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

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

volkingCommented:
       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
            Next
        Next
0
volkingCommented:
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);
        }
    }
}
0
newbiewebSr. Software EngineerAuthor Commented:
Yes, I can use Linq. The other code seems good but brute force.  What can you suggest wth Linq?
0
käµfm³d 👽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

0

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
käµfm³d 👽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

0
newbiewebSr. Software EngineerAuthor Commented:
Nice code. Thanks,
0
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
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.