Solved

Need to compare two arrays of any primitive type for equality

Posted on 2014-10-16
2
124 Views
Last Modified: 2014-11-05
I need a method that will compare two arrays of any primitive type for the same contents.  Any order.
0
Comment
Question by:Bruce
2 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 40386149
Imho there is no way to tell, whether you have a sequential memory layout, thus you should stick to SequenceEqual.
0
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 40386251
you may try

bool arraysAreEqual = (array1.Length == array2.Length) 
                     && new HashSet<T>(array1).SetEquals(array2);

Open in new window


in case the arrays haven't duplicate values.

if duplicates could happen use

SortedDictionary<T, int> dict1 = new SortedDictionary<T, int>();
SortedDictionary<T, int> dict2 = new SortedDictionary<T, int>();
foreach (t in array1) 
     dict1[t]++;  // count occurences
foreach (t in array2) 
     dict2[t]++; // count occurences
bool areArraysEqual = dict1.SequenceEqual(dict2);

Open in new window

or use a function like

static bool AreArraysEqual<T>(T[] array1, T[] array2)
{
    if (array1.Length != array2.Length)
        return false;
    Array.Sort(array1);
    Array.Sort(array2);

    EqualityComparer<T> comparer = EqualityComparer<T>.Default;
    for (int i = 0; i < array1.Length; i++)
    {
        if (!comparer.Equals(array1[i], array2[i])) return false;
    }
    return true;}

Open in new window


Sara
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now