Compare two list in c# using linq
Here we’ll see how to write a C program to compare two linked lists. First we’ll create three linked lists, first two are equal and third one is different. We’ll compare these linked lists using the check_equal() function. Show The check_equal() function traverses the linked lists until at least one of them reaches to NULL (end). If the value fields of two linked list are not equal at any point, the function returns 0 (not equal). After traversal, if any one list does not reach to NULL (end), then it also returns 0. Next two lines check that. If both lists reach to NULL after traversal and values of all nodes are equal, it returns 1 (equal). Here is the output of the program. Recursive VersionHere is the recursive function of the check_equal() function. int check_equal(struct node *head1, struct node *head2) { if (head1 == NULL && head2 == NULL) return 1; if(head1 != NULL && head2 != NULL) { if(head1->val != head2->val) return 0; return check_equal(head1->next, head2->next); } return 0; }If you also want to contribute, click here.
CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900
This example shows how to use LINQ to compare two lists of strings and output those lines that are in names1.txt but not in names2.txt. To create the data filesExampleclass CompareLists { static void Main() { // Create the IEnumerable data sources. string[] names1 = System.IO.File.ReadAllLines(@"../../../names1.txt"); string[] names2 = System.IO.File.ReadAllLines(@"../../../names2.txt"); // Create the query. Note that method syntax must be used here. IEnumerableSome types of query operations in C#, such as Except, Distinct, Union, and Concat, can only be expressed in method-based syntax. Compiling the CodeCreate a C# console application project, with using directives for the System.Linq and System.IO namespaces. See also
This post will discuss how to compare two lists in C#. Two lists are equivalent if they have the same elements in the same quantity but any order. Individual elements are equal if their values are equal, not if they refer to the same object. 1. Compare two List objects for equality, with regard to orderIf the ordering of elements matters, we can simply use LINQ’s SequenceEqual() method, which determines whether two sequences are equal according to an equality comparer.
Download Run Code 2. Compare two List objects for equality, ignoring orderTo ensure both lists have exactly the same set of elements regardless of order, we can sort both lists and then compare them for equality using the SequenceEqual() method.
Download Run Code The following example demonstrates the usage of the CollectionAssert.AreEquivalent() method.
Download Run Code That’s all about comparing two lists for equality in C#.
Thanks for reading. Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages. Like us? Refer us to your friends and help us grow. Happy coding 🙂 |