1

I've got two string arrays. I want to select one element from the first array and compare to each element of the second array. If element from first array exist in elements of second array i whant to write for example ("Element exist") or something like this.

This should be possible to do with two for loops ?

EDIT

Ok i finaly achived what i wanted usign this code:

string[] ArrayA = { "dog", "cat", "test", "ultra", "czkaka", "laka","kate" };
string[] ArrayB = { "what", "car", "test", "laka","laska","kate" };

bool foundSwith = false;

for (int i = 0; i < ArrayA.Length; i++)
{

   for (int j = 0; j < ArrayB.Length; j++)
   {
       if (ArrayA[i].Equals(ArrayB[j]))
       {
           foundSwith = true;
           Console.WriteLine("arrayA element: " + ArrayA[i] + " was FOUND in arrayB");
       }
   }

   if (foundSwith == false)
   {
      Console.WriteLine("arrayA element: " + ArrayA[i] + " was NOT found in arrayB");
   }
   foundSwith = false;
}

I hope this will help others who will want to compare two arrays ;). its all about this foundSwitch. Thx for help once again.

5
  • 2
    "This should be possible to do with two for loops ?" Absolutely! Did you try writing these two loops? How did it go? Commented Jul 15, 2012 at 12:33
  • 1
    It probably takes longer to write this post than to write a script to test this. #lazy Commented Jul 15, 2012 at 12:45
  • Sorry about not giving a code. I edited first post with my code. Commented Jul 15, 2012 at 12:50
  • You can replace else if (s1 != s2) with only else... and it will work fine... what is the problem then? Commented Jul 15, 2012 at 14:15
  • Could you check out my example in main question ? I just added to clarify what im trying to do. When im using else instead of else if i got the same wrong result (just like i write in example in main question) Commented Jul 15, 2012 at 14:26

3 Answers 3

5
foreach (string str in yourFirstArray)
{
   if (yourSearchedArray.Contains(str))
   {
      Console.WriteLine("Exists");
   }
}
Sign up to request clarification or add additional context in comments.

6 Comments

This is a good solution, but you should not create the List<> it creates a new copy of the array which is unnecessary overhead. You could just use yourStringArray.Contains(str) directly.
Im using .NET framework 2.0 and o got errors with this Even when i add using System.Linq.
can you try using 'Array.Exists()` instead of Contains? (msdn.microsoft.com/en-us/library/yw84x8be(v=vs.80))
i cannot unfortunately. Im trying to use code from belowe comment: foreach (string str in strArray) { foreach (string str2 in strArray2) { if (str == str2) { Console.WriteLine("element exists"); }else { Console.WriteLine ("element doesnt exists"); } } But i also need else statmant to write (this element does not exists). But when i write else stataments it overiterates i dont know why
can you update your code with the new version and explain the problem again?
|
1
foreach (string str in strArray)
{
   foreach (string str2 in strArray2)
   {
       if (str == str2)
       {
          Console.WriteLine("element exists");
       }
   }
}

Updated to display when string does not exist in strArray2

bool matchFound = false;
foreach (string str in strArray)
    {
       foreach (string str2 in strArray2)
       {
           if (str == str2)
           {
              matchFound = true;
              Console.WriteLine("a match has been found");
           }
       }

       if (matchFound == false)
       {
          Console.WriteLine("no match found");
       }
    }

Or another way of doing this in less lines of code:

foreach (string str in strArray)
{
    if(strArray2.Contains(str))
    {
       Console.WriteLine("a match has been found");
    }
    else
    {
       Console.WriteLine("no match found");
    }
}

1 Comment

Its good but when i add to this if and else statmant for displaying this element not exist i receive weird result. Like its over iterating. How to add to this functionality - when element exists it writes element exists but when not element does not exists ?
-1

You can also try:

ArrayA.All(ArrayB.Contains);

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.