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.

#include #include struct node{ int val; struct node *next; }; int check_equal[struct node *head1, struct node *head2] { while[head1 != NULL && head2 != NULL] { if[head1->val != head2->val] return 0; head1 = head1->next; head2 = head2->next; } if [head1 != NULL] return 0; if [head2 != NULL] return 0; return 1; } void print_list[struct node *head] { printf["H->"]; while[head] { printf["%d->", head->val]; head = head->next; } printf["|||\n\n"]; } void insert_front[struct node **head, int value] { struct node * new_node = NULL; new_node = [struct node *]malloc[sizeof[struct node]]; if [new_node == NULL] { printf["Failed to insert element. Out of memory"]; } new_node->val = value; new_node->next = *head; *head = new_node; } int main[] { struct node * head1 = NULL; struct node * head2 = NULL; struct node * head3 = NULL; /*Creating the first linked list*/ insert_front[&head1, 16]; insert_front[&head1, 83]; insert_front[&head1, 89]; insert_front[&head1, 12]; insert_front[&head1, 67]; insert_front[&head1, 20]; /*Creating the second linked list*/ insert_front[&head2, 16]; insert_front[&head2, 83]; insert_front[&head2, 89]; insert_front[&head2, 12]; insert_front[&head2, 67]; insert_front[&head2, 20]; /*Creating the third linked list*/ insert_front[&head3, 16]; insert_front[&head3, 83]; insert_front[&head3, 50]; insert_front[&head3, 12]; insert_front[&head3, 67]; insert_front[&head3, 20]; printf["Linked List 1: "]; print_list[head1]; printf["Linked List 2: "]; print_list[head2]; printf["Linked List 3: "]; print_list[head3]; printf["\nChecking Linked List 1 and Linked List 2...\n"]; if[check_equal[head1, head2]] { printf["Linked List 1 and Linked List 2 are equal.\n"]; } else { printf["Linked List 1 and Linked List 2 are not equal.\n"]; } printf["\nChecking Linked List 1 and Linked List 3...\n"]; if[check_equal[head1, head3]] { printf["Linked List 1 and Linked List 3 are equal.\n"]; } else { printf["Linked List 1 and Linked List 3 are not equal.\n"]; } return 0; }

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 Version

Here 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

  • Article
  • 03/11/2022
  • 2 minutes to read

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 files

Example

class 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. IEnumerable differenceQuery = names1.Except[names2]; // Execute the query. Console.WriteLine["The following lines are in names1.txt but not names2.txt"]; foreach [string s in differenceQuery] Console.WriteLine[s]; // Keep the console window open until the user presses a key. Console.WriteLine["Press any key to exit"]; Console.ReadKey[]; } } /* Output: The following lines are in names1.txt but not names2.txt Potra, Cristina Noriega, Fabricio Aw, Kam Foo Toyoshima, Tim Guy, Wey Yuan Garcia, Debra */

Some types of query operations in C#, such as Except, Distinct, Union, and Concat, can only be expressed in method-based syntax.

Compiling the Code

Create 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 order

If 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.

using System.Collections.Generic;

    public static void Main[]

        List x = new List[] {3, 5, 3, 2, 7};

        List y = new List[] {3, 5, 3, 7, 2};

        bool isEqual = Enumerable.SequenceEqual[x, y];

            Console.WriteLine["Lists are Equal"];

            Console.WriteLine["Lists are not Equal"];

    Output: Lists are not Equal

Download  Run Code

2. Compare two List objects for equality, ignoring order

To 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.

using System.Collections.Generic;

    public static void Main[]

        List x = new List[] {3, 5, 3, 2, 7};

        List y = new List[] {3, 5, 3, 7, 2};

        bool isEqual = Enumerable.SequenceEqual[x.OrderBy[e => e], y.OrderBy[e => e]];

            Console.WriteLine["Lists are Equal"];

            Console.WriteLine["Lists are not Equal"];

Download  Run Code

 
Microsoft Testing Framework has the CollectionAssert.AreEquivalent[] method included in the Microsoft.VisualStudio.TestTools.UnitTesting namespace, which tests whether two lists contain the same elements, without regard to order. It throws an exception when either list contains an element not in the other list.

The following example demonstrates the usage of the CollectionAssert.AreEquivalent[] method.

using System.Collections.Generic;

using Microsoft.VisualStudio.TestTools.UnitTesting;

    public static void Main[]

        List x = new List[] { 3, 5, 3, 2, 7 };

        List y = new List[] { 3, 5, 3, 2, 7 };

            CollectionAssert.AreEquivalent[x, y];

            Console.WriteLine["Both Sequences have same elements"];

        catch [AssertFailedException] {

            Console.WriteLine["Both Sequences have different elements"];

    Output: Both Sequences have same elements

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 🙂


Video liên quan

Chủ Đề