I have two arrays (same length), and I'm trying to iterate each element in both arrays and check if those elements (having the same index ) are equal or not. I want to get a number equal, and a number that is not equal. I was able to do this with a loop while, but I wonder if there is a faster way with extensions System.Linq?
Here is what I have right now:
var list1 = new List<Color>
{
Color.Aqua,
Color.AliceBlue,
Color.Beige,
Color.Red,
Color.Green,
Color.White
};
var list2 = new List<Color>
{
Color.Red,
Color.BurlyWood,
Color.Beige,
Color.Azure,
Color.Green,
Color.Magenta
};
var enumFirst = list1.GetEnumerator();
var enumSecond = list2.GetEnumerator();
var equal = 0;
var notEqual = 0;
while (enumFirst.MoveNext() && enumSecond.MoveNext())
{
var colorFirst = enumFirst.Current;
var colorSecond = enumSecond.Current;
if (colorFirst == colorSecond)
equal++;
else
notEqual++;
}
Console.WriteLine("Elements that are equal: " + equal);
Console.WriteLine("Elements that are not equal: " + notEqual);
Output:
Elements that are equal: 2
Elements that are not equal: 4
source
share