3

I have two string arrays that hold values. How can i check if the first array contains an element that is also in the second array? I want to make a loop that checks through if there are any elements that are same in both, then i want to use that value and and display it in a message box. How do i compare them like that?

string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
string[] potenDays = { "Mon", "Tue", "None", "None", "None", "None", "None" };

2 Answers 2

7

Use Intersect

var both =  weekDays.Intersect(potenDays);
var count = both.Count();
var daysArray = both.ToArray();
Sign up to request clarification or add additional context in comments.

Comments

0
foreach (var weekDay in weekDays.Where(wd => potenDays.Contains(wd)))
{
    // Show weekDay
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.