C#: How to compare two arrays

Also interesting to point out the syntactical differences in this post compared to the previous one. I didn’t even have to lower case the “S” in String, but I did never the less just because I prefer the color.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace twoArray
{
class Program
{
static void Main(string[] args)
{
//populate the arrays
string [] arrayA =
{“dog”,”cat”,”wolf”,”flamingo”,”spider”,”?”,”7″,”orange”};
string [] arrayB =
{“fox”,”wolf”,”bear”, “7”,”cheese”,”spider”,”paper”,”cat”};

//loop through the contents of arrayA
//and comparing each element to each element in arrayB
//switch variable used to indicate whether a match was found
bool foundSwitch = false;

//outer loop for all the elements in arrayA[i]
for(int i = 0; i < arrayA.Length; i++)
{
//inner loop for all the elements in arrayB[j]
for (int j = 0; j < arrayB.Length;j++)
{
//compare arrayA to arrayB and output results
if( arrayA[i]== arrayB[j])
{
foundSwitch = true;
Console.WriteLine( “arrayA element \”” + arrayA[i] + “\” was found in arrayB” );
}
}
if (foundSwitch == false)
{
Console.WriteLine( “arrayA element \”” + arrayA[i] + “\” was Not found in arrayB” );
}
//set foundSwitch bool back to false
foundSwitch = false;
}
}
}
}

3 thoughts on “C#: How to compare two arrays

  1. somayeh

    Suppose 3. kind C, B, A, each having 50 elements A and B are in element arrays The values are stored. Write a program similar elements) and address (represented by A, B together in c#

    Reply

Leave a Reply to abc Cancel reply

Your email address will not be published.