Examining two string arrays for equivalence

前端 未结 5 1384
无人共我
无人共我 2021-01-21 21:38

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[] s         


        
相关标签:
5条回答
  • 2021-01-21 21:53

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

    0 讨论(0)
  • 2021-01-21 21:55

    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;
    }
    
    0 讨论(0)
  • 2021-01-21 21:59

    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

    0 讨论(0)
  • 2021-01-21 22:06

    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.

    0 讨论(0)
  • 2021-01-21 22:07

    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.

    0 讨论(0)
提交回复
热议问题