Solved

Need to compare two arrays of any primitive type for equality

Posted on 2014-10-16
2
122 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 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
Imho there is no way to tell, whether you have a sequential memory layout, thus you should stick to SequenceEqual.
0
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

8 Experts available now in Live!

Get 1:1 Help Now