Grokodile
Grokodile

Reputation: 3933

Examining two string arrays for equivalence

Is there a better way to examine whether two string arrays have the same contents than this?

string[] first = new string[]{"cat","and","mouse"};
string[] second = new string[]{"cat","and","mouse"};

bool contentsEqual = true;

if(first.Length == second.Length){
    foreach (string s in first)
    {
        contentsEqual &= second.Contains(s);
    }
}
else{
    contentsEqual = false;
}


Console.WriteLine(contentsEqual.ToString());//    true

Upvotes: 2

Views: 1004

Answers (5)

Chris Laplante
Chris Laplante

Reputation: 29658

You could try Enumerable.Intersect: http://msdn.microsoft.com/en-us/library/bb460136.aspx

The result of the operation is every element that is common to both arrays. If the length of the result is equal to the length of both arrays, then the two arrays contain the same items.

Enumerable.Union: http://msdn.microsoft.com/en-us/library/bb341731.aspx would work too; just check that the result of the Union operation has length of zero (meaning there are no elements that are unique to only one array);

Although I'm not exactly sure how the functions handle duplicates.

Upvotes: 1

Noldorin
Noldorin

Reputation: 147240

Nothing wrong with the logic of the method, but the fact that you're testing Contains for each item in the first sequence means the algorithm runs in O(n^2) time in general. You can also make one or two other smaller optimisations and improvements

I would implement such a function as follows. Define an extension method as such (example in .NET 4.0).

public static bool SequenceEquals<T>(this IEnumerable<T> seq1, IEnumerable<T> seq2)
{
    foreach (var pair in Enumerable.Zip(seq1, seq2)
    {
        if (!pair.Item1.Equals(pair.Item2))
            return;
    }
    return false;
}

Upvotes: 1

IVlad
IVlad

Reputation: 43477

This is O(n^2). If the arrays have the same length, sort them, then compare elements in the same position. This is O(n log n).

Or you can use a hash set or dictionary: insert each word in the first array, then see if every word in the second array is in the set or dictionary. This is O(n) on average.

Upvotes: 2

spinon
spinon

Reputation: 10847

You should consider using the intersect method. It will give you all the matching values and then you can just compare the count of the resulting array with one the arrays that were compared.

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.intersect.aspx

Upvotes: 2

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68667

Enumerable.SequenceEquals if they're supposed to be in the same order.

Upvotes: 6

Related Questions