Finding the overlap in two lists - CSharp Collection

CSharp examples for Collection:List

Description

Finding the overlap in two lists

Demo Code

using System;//  ww  w  . java 2 s. c om
using System.Collections.Generic;
class Program
{
   static void Main(string[] args)
   {
      List<string> presidentialCandidates = new List<string> { "B", "C", "D", "T", "E", "X" };
      List<string> senators = new List<string> { "A", "QQ", "B", "T", "E", "N" };
      // Following set hasn't yet weeded out non-Senators ...
      HashSet<string> senatorsRunning = new HashSet<string>(presidentialCandidates);
      senatorsRunning.IntersectWith(senators);
      foreach (string senator in senatorsRunning)
      {
         Console.WriteLine(senator);
      }
   }
}

Result


Related Tutorials