Tuesday, September 22, 2009

Find common elements in c# generic list

The following code demonstrates how to get the common elements in
given 2 lists using Intersect method of System.Collections.Generic.List.


using
System;
using System.Linq;

namespace ConsoleApplication1
{
static class Program
{
static void Main(string[] args)
{
int[] elementSet1 = { 5, 1, 6, 3, 8 };
int[] elementSet2 = { 3, 7, 8, 6, 5 };

foreach (int element in elementSet1.Intersect(
elementSet2))
{
Console.WriteLine(element);
}
Console.Read();
}
}
}


kick it on DotNetKicks.com

1 comments:

Kiran said...

nice...got my work done