C# program to find Union of two or more Lists



Firstly, create lists −

//three lists var list1 = new List{3, 4, 5}; var list2 = new List{1, 2, 3, 4, 5}; var list3 = new List{5, 6, 7, 8};

Use the union method to get the union of list1 and list2 −

var res1 = list1.Union(list2); var res2 = res1.Union(list3);

The following is the complete code −

Example

 Live Demo

using System.Collections.Generic; using System.Linq; using System; public class Demo {    public static void Main() {       //three lists       var list1 = new List<int>{3, 4, 5};       var list2 = new List<int>{1, 2, 3, 4, 5};       var list3 = new List<int>{5, 6, 7, 8};       // finding union       var res1 = list1.Union(list2);       var res2 = res1.Union(list3);       foreach(int i in res2) {          Console.WriteLine(i);       }    } }

Output

3 4 5 1 2 6 7 8

Updated on: 2020-06-22T13:01:22+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements