Working with sets

Sets are one of the most fundamental concepts in mathematics. A set is a collection of one or more objects. You might remember being taught about Venn diagrams in school. Common set operations include the intersect or union between sets.

Add a new console application project named Ch09_Sets in either Visual Studio 2017 or Visual Studio Code.

In Visual Studio 2017, set the solution's start up project to be the current selection.

This application will define three arrays of strings for cohorts of apprentices and then perform some common set operations.

Import the following additional namespaces:

    using System.Collections.Generic; // for IEnumerable<T> 
    using System.Linq; // for LINQ extension methods 

Inside the Program class, before the Main method, add the following method that outputs any sequence of string variables as a comma-separated single string to the console output along with an optional description:

    private static void Output( 
      IEnumerable<string> cohort, string description = "") 
      { 
        if (!string.IsNullOrEmpty(description)) 
        { 
          WriteLine(description); 
        } 
        Write("  "); 
        WriteLine(string.Join(", ", cohort.ToArray())); 
      } 

In the Main method, write the following statements:

    var cohort1 = new string[]  
      { "Rachel", "Gareth", "Jonathan", "George" }; 
    var cohort2 = new string[]  
      { "Jack", "Stephen", "Daniel", "Jack", "Jared" }; 
    var cohort3 = new string[]  
      { "Declan", "Jack", "Jack", "Jasmine", "Conor" }; 
 
    Output(cohort1, "Cohort 1"); 
    Output(cohort2, "Cohort 2"); 
    Output(cohort3, "Cohort 3"); 
    WriteLine(); 
 
    Output(cohort2.Distinct(), "cohort2.Distinct(): removes
    duplicates"); 
    Output(cohort2.Union(cohort3), "cohort2.Union(cohort3): combines
    two sequences and removes any duplicates"); 
    Output(cohort2.Concat(cohort3), "cohort2.Concat(cohort3): combines
    two sequences but leaves in any duplicates"); 
    Output(cohort2.Intersect(cohort3), "cohort2.Intersect(cohort3):
    returns items that are in both sequences"); 
    Output(cohort2.Except(cohort3), "cohort2.Except(cohort3): removes
    items from the first sequence that are in the second sequence"); 
    Output(cohort1.Zip(cohort2, (c1, c2) => $"{c1} matched with
    {c2}"), "cohort1.Zip(cohort2, (c1, c2) => $"{c1} matched with
    {c2}"): matches items based on position in the sequence"); 

Run the console application and view the output:

Cohort 1
Rachel, Gareth, Jonathan, George
Cohort 2
Jack, Stephen, Daniel, Jack, Jared
Cohort 3
Declan, Jack, Jack, Jasmine, Conor
cohort2.Distinct(): removes duplicates
Jack, Stephen, Daniel, Jared
cohort2.Union(cohort3): combines two sequences and removes any
duplicates
Jack, Stephen, Daniel, Jared, Declan, Jasmine, Conor
cohort2.Concat(cohort3): combines two sequences but leaves in any
duplicates
Jack, Stephen, Daniel, Jack, Jared, Declan, Jack, Jack, Jasmine,
  Conor
cohort2.Intersect(cohort3): returns items that are in both sequences
Jack
cohort2.Except(cohort3): removes items from the first sequence that
are in the second sequence
Stephen, Daniel, Jared
cohort1.Zip(cohort2, (c1, c2) => $"{c1} matched with {c2}"): matches
items based on position in the sequence
Rachel matched with Jack, Gareth matched with Stephen, Jonathan    
  matched with Daniel, George matched with Jack

Note

With Zip, if there are unequal numbers of items in the two sequences, then some items will not have a matching partner.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.129.253