Easiest Way to Compare Arrays in C#

Easiest way to compare arrays in C#

You could use Enumerable.SequenceEqual. This works for any IEnumerable<T>, not just arrays.

Compare Arrays in C# using SequenceEqual

Using @Sweeper/@Jeppe Stig Nielsen suggestions, I rewrote my function as:

public bool Equals(VRValue<T> other)
{
if (typeof(T).IsArray)
{
var first = Value as IStructuralEquatable;
var second = other.Value as IStructuralEquatable;
return StructuralComparisons.StructuralEqualityComparer.Equals(first, second);
}
[...]
}

When using a HashSet, one should also pay attention to GetHashCode:

public override int GetHashCode()
{
if (typeof(T).IsArray)
{
var first = Value as IStructuralEquatable;
return StructuralComparisons.StructuralEqualityComparer.GetHashCode(first);
}
[...]
}

What's the fastest way to compare two arrays for equality?

I can't guarantee that this is the fastest, but it's certainly quite efficient:

bool areEquivalent = array1.Length == array2.Length 
&& new HashSet<string>(array1).SetEquals(array2);

EDIT:
SaeedAlg and Sandris raise valid points about different frequencies of duplicates causing problems with this approach. I can see two workarounds if this is important (haven't given much thought to their respective efficiencies):

1.Sort the arrays and then compare them sequentially. This approach, in theory, should have quadratic complexity in the worst case.
E.g.:

return array1.Length == array2.Length
&& array1.OrderBy(s => s).SequenceEqual(array2.OrderBy(s => s));

2.Build up a frequency-table of strings in each array and then compare them. E.g.:

if(array1.Length != array2.Length)
return false;

var f1 = array1.GroupBy(s => s)
.Select(group => new {group.Key, Count = group.Count() });

var f2 = array2.GroupBy(s => s)
.Select(group => new {group.Key, Count = group.Count() });

return !f1.Except(f2).Any();

Comparing two arrays in C#

Since this is Homework, I will give you a homework answer.

Sure, you could use LINQ and rely on SequenceEqual, Intersect, etc, but that is likely not the point of the exercise.

Given two arrays, you can iterate over the elements in an array using foreach.

int[] someArray;
foreach(int number in someArray)
{
//number is the current item in the loop
}

So, if you have two arrays that are fairly small, you could loop over each number of the first array, then loop over the all the items in the second array and compare. Let's try that. First, we need to correct your array syntax. It should look something like this:

    int[] a = new int[] {1, 2, 3, 4};
int[] b = new int[] { 5, 6, 1, 2, 7, 8 };

Note the use of the curly braces {. You were using the syntax to create a N-dimensional array.

bool hasDuplicate = false;
int[] a = new int[] { 1, 2, 3, 4 };
int[] b = new int[] { 5, 6, 7, 8 };
foreach (var numberA in a)
{
foreach (var numberB in b)
{
//Something goes here
}
}

This gets us pretty close. I'd encourage you to try it on your own from here. If you still need help, keep reading.


OK, so we basically need to just check if the numbers are the same. If they are, set hasDuplicate to true.

bool hasDuplicate = false;
int[] a = new int[] { 8, 1, 2, 3, 4 };
int[] b = new int[] { 5, 6, 7, 8 };
foreach (var numberA in a)
{
foreach (var numberB in b)
{
if (numberA == numberB)
{
hasDuplicate = true;
}
}
}

This is a very "brute" force approach. The complexity of the loop is O(n2), but that may not matter in your case. The other answers using LINQ are certainly more efficient, and if efficiency is important, you could consider those. Another option is to "stop" the loops using break if hasDuplicate is true, or place this code in a method and use return to exit the method.

Compare arrays of int in high performance

One array must be sorted so that you can compare in n*log(n). That is for every item in the unsorted array (n) you perform a binary search on the sorted array (log(n)). If both are unsorted, I don't see a way to compare in n*log(n).



Related Topics



Leave a reply



Submit