

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to compare two lists and add the difference to a third list in C#?
First, set the two lists −
List One
List < string > list1 = new List < string > (); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D");
List Two
List < string > list2 = new List < string > (); list2.Add("C"); list2.Add("D");
To find the difference between the two list and display the difference elements −
IEnumerable < string > list3; list3 = list1.Except(list2); foreach(string value in list3) { Console.WriteLine(value); }
The following is the complete example to compare two lists −
Example
using System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List < string > list1 = new List < string > (); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D"); Console.WriteLine("First list..."); foreach(string value in list1) { Console.WriteLine(value); } Console.WriteLine("Second list..."); List < string > list2 = new List < string > (); list2.Add("C"); list2.Add("D"); foreach(string value in list2) { Console.WriteLine(value); } Console.WriteLine("Difference in the two lists..."); IEnumerable < string > list3; list3 = list1.Except(list2); foreach(string value in list3) { Console.WriteLine(value); } } }
- Related Questions & Answers
- How to compare two lists in Python?
- C# program to list the difference between two lists
- Python program to list the difference between two lists.
- How to compare two lists for equality in C#?
- Multiply two numbers represented as linked lists into a third list in C++
- How do we compare two lists in Python?
- How do we compare the elements of two lists in Python?
- How do you add two lists in Java?
- Add two numbers represented by linked lists?
- Python Program to Put Even and Odd elements in a List into Two Different Lists
- Compare two arrays of single characters and return the difference? JavaScript
- How to join two lists in C#?
- How to add third level of ticks in Python Matplotlib?
- Program to find minimum difference between two elements from two lists in Python
- Python program to create a sorted merged list of two unsorted lists
Advertisements