Home
Map
SequenceEqual MethodUse the SequenceEqual extension method from System.Linq to compare two collections.
C#
This page was last reviewed on Sep 7, 2023.
SequenceEqual. Are two sequences the same? With the SequenceEqual extension from System.Linq in the C# language, you can test 2 collections for equality in one statement.
Method notes. SequenceEqual compares 2 collections for exact equality. Its performance is much worse than alternative implementations. We look at the SequenceEqual method.
IEnumerable
List
First example. To begin, SequenceEqual is strict in its comparison: ordering, and the number of elements is important. Everything must be the same for a true result.
Array
Part 1 When 2 arrays have the exact same elements in the same order, we find they are equal. SequenceEqual returns true.
Part 2 If an additional element is at the end of one of the elements, we find that SequenceEqual returns false.
Tip Try changing one of the "cat" and "bird" arrays to have elements in the opposite order. SequenceEqual will return false.
using System; using System.Linq; // Part 1: two arrays are equal. string[] array1 = { "cat", "bird" }; string[] array2 = { "cat", "bird" }; Console.WriteLine(array1.SequenceEqual(array2)); // Part 2: all elements must be equal, no extra elements are allowed. string[] array3 = { "cat", "bird", "frog" }; Console.WriteLine(array1.SequenceEqual(array3));
True False
Benchmark. Methods found in System.Linq usually have worse performance on arrays than custom imperative algorithms. I tested the performance of SequenceEqual.
Version 1 This version of the code uses SequenceEqual to compare 2 larger-sized integer arrays.
int Array
Version 2 Here we use a for-loop method called ArraysEqual to compare the arrays.
for
Result SequenceEqual was more than ten times slower than the imperative algorithm (ArraysEqual).
Note It's good to keep in mind that SequenceEqual on large equal arrays is more than ten times slower than a loop.
using System; using System.Diagnostics; using System.Linq; class Program { static void Main() { const int max = 10000; var a1 = Enumerable.Range(0, short.MaxValue).ToArray(); var a2 = Enumerable.Range(0, short.MaxValue).ToArray(); var s1 = Stopwatch.StartNew(); // Version 1: use SequenceEqual. for (int i = 0; i < max; i++) { bool equals = a1.SequenceEqual(a2); } s1.Stop(); var s2 = Stopwatch.StartNew(); // Version 2: use custom method. for (int i = 0; i < max; i++) { bool equals = ArraysEqual(a1, a2); } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / max).ToString("0.00 ns")); } static bool ArraysEqual(int[] a1, int[] a2) { if (a1.Length == a2.Length) { for (int i = 0; i < a1.Length; i++) { if (a1[i] != a2[i]) { return false; } } return true; } return false; } }
515889.45 ns 36280.79 ns
A summary. SequenceEqual is an easy way to compare 2 arrays or other collections such as Lists for equality. Any argument to SequenceEqual must implement the IEnumerable interface.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 7, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.