0
0
C Sharp (C#)programming~30 mins

Set operations (Union, Intersect, Except) in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Set operations (Union, Intersect, Except) in C#
📖 Scenario: You are managing two lists of favorite fruits from two friends. You want to find fruits they both like, fruits either one likes, and fruits only one friend likes.
🎯 Goal: Build a C# program that uses set operations Union, Intersect, and Except on two lists of fruits.
📋 What You'll Learn
Create two lists of strings named friend1Fruits and friend2Fruits with exact fruits given.
Create a variable allFruits that is the union of both lists.
Create a variable commonFruits that is the intersection of both lists.
Create a variable uniqueToFriend1 that contains fruits only in friend1Fruits but not in friend2Fruits.
Print the results exactly as instructed.
💡 Why This Matters
🌍 Real World
Set operations help compare collections like user preferences, inventory lists, or survey answers.
💼 Career
Understanding set operations is useful for data analysis, filtering data, and combining information efficiently in software development.
Progress0 / 4 steps
1
Create the fruit lists
Create two lists of strings called friend1Fruits and friend2Fruits. Set friend1Fruits to contain "apple", "banana", and "cherry". Set friend2Fruits to contain "banana", "dragonfruit", and "apple".
C Sharp (C#)
Need a hint?

Use List<string> and initialize with the exact fruits inside curly braces.

2
Create the union set
Create a variable called allFruits that holds the union of friend1Fruits and friend2Fruits using the Union method and convert it to a list.
C Sharp (C#)
Need a hint?

Use friend1Fruits.Union(friend2Fruits).ToList() to get all unique fruits.

3
Create the intersection and difference sets
Create a variable called commonFruits that holds the intersection of friend1Fruits and friend2Fruits using the Intersect method and convert it to a list. Also create a variable called uniqueToFriend1 that holds fruits only in friend1Fruits but not in friend2Fruits using the Except method and convert it to a list.
C Sharp (C#)
Need a hint?

Use Intersect for common fruits and Except for fruits unique to friend1.

4
Print the results
Print the contents of allFruits, commonFruits, and uniqueToFriend1 each on a separate line. Use string.Join(", ", ...) to join the list items. Print exactly as follows:
All fruits: apple, banana, cherry, dragonfruit
Common fruits: apple, banana
Unique to friend1: cherry
C Sharp (C#)
Need a hint?

Use Console.WriteLine and string.Join to print the lists as comma-separated strings.