Solved

Need to compare two arrays of any primitive type for equality

Posted on 2014-10-16
2
129 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 34

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 34

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

615 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