Menu
Coddy logo textTech

Set Union

Part of the Logic & Flow section of Coddy's Dart journey — lesson 22 of 65.

When you have two separate Set collections, you can combine them into a single set containing all unique elements from both using the union() method. This operation creates a new set that includes every element that appears in either of the original sets.

The union() method automatically handles duplicates for you. If the same element exists in both sets, it will appear only once in the resulting union set, maintaining the fundamental property that sets contain only unique values.

Set<String> frontendSkills = {'HTML', 'CSS', 'JavaScript'};
Set<String> backendSkills = {'JavaScript', 'Python', 'SQL'};

Set<String> allSkills = frontendSkills.union(backendSkills);
print(allSkills);  // {HTML, CSS, JavaScript, Python, SQL}

Notice how 'JavaScript' appears in both original sets but only once in the union result. This makes union() perfect for combining related collections while ensuring no duplicates, such as merging skill sets, combining user permissions, or consolidating data from multiple sources.

To find elements that appear in both sets, use the intersection() method. It returns a new set containing only the shared elements:

Set<String> sharedSkills = frontendSkills.intersection(backendSkills);
print(sharedSkills);  // {JavaScript}

Once you have a set of overlapping elements, you can convert it to a readable string using the join() method. This works the same way as on a List — it joins all elements into a single string with a separator you provide:

Set<String> overlap = frontendSkills.intersection(backendSkills);
String result = overlap.isEmpty ? 'None' : overlap.join(', ');
print(result);  // JavaScript

Together, union(), intersection(), and join() give you powerful tools for comparing and displaying set data — for example, combining team skill sets and identifying which skills they already share.

challenge icon

Challenge

Easy

Create a program that manages a professional networking system by combining skill sets from different team members to create a comprehensive project skill profile. Your program should:

  1. Read a string input representing the project name
  2. Read multiple string inputs representing the first team member's skills (the input will end when you receive "member1_done")
  3. Read multiple string inputs representing the second team member's skills (the input will end when you receive "member2_done")
  4. Create two separate Sets to store each team member's skills
  5. Use the union() method to combine both skill sets into a comprehensive project skill profile
  6. Print the skill combination results in the exact format shown below

For example, if the project name is "Web Development Project", the first member's skills are "HTML", "CSS", "JavaScript", "React", and the second member's skills are "JavaScript", "Node.js", "MongoDB", "React", your program should output:

Project: Web Development Project
Team Member 1 skills: {HTML, CSS, JavaScript, React}
Team Member 2 skills: {JavaScript, Node.js, MongoDB, React}
Combined project skills: {HTML, CSS, JavaScript, React, Node.js, MongoDB}
Total unique skills available: 6
Overlapping skills: JavaScript, React
Status: Project skill profile created successfully

If the project name is "Mobile App Development", the first member's skills are "Dart", "Flutter", "Firebase", and the second member's skills are "Swift", "iOS", "Xcode", your program should output:

Project: Mobile App Development
Team Member 1 skills: {Dart, Flutter, Firebase}
Team Member 2 skills: {Swift, iOS, Xcode}
Combined project skills: {Dart, Flutter, Firebase, Swift, iOS, Xcode}
Total unique skills available: 6
Overlapping skills: None
Status: Project skill profile created successfully

If the project name is "Data Science Project", the first member's skills are "Python", "Pandas", "NumPy", "SQL", and the second member's skills are "Python", "SQL", "Tableau", "Statistics", your program should output:

Project: Data Science Project
Team Member 1 skills: {Python, Pandas, NumPy, SQL}
Team Member 2 skills: {Python, SQL, Tableau, Statistics}
Combined project skills: {Python, Pandas, NumPy, SQL, Tableau, Statistics}
Total unique skills available: 6
Overlapping skills: Python, SQL
Status: Project skill profile created successfully

Your program must use the union() method to combine the two skill sets, demonstrating how it automatically handles duplicates. Show the individual skill sets first, then the combined result. Identify any overlapping skills by finding skills that appear in both original sets. If there are no overlapping skills, display "None" for the overlapping skills line.

Try it yourself

import 'dart:io';

void main() {
  // Read project name
  String? projectName = stdin.readLineSync();
  
  // Read first team member's skills
  Set<String> member1Skills = <String>{};
  String? skill;
  while ((skill = stdin.readLineSync()) != "member1_done") {
    if (skill != null) {
      member1Skills.add(skill);
    }
  }
  
  // Read second team member's skills
  Set<String> member2Skills = <String>{};
  while ((skill = stdin.readLineSync()) != "member2_done") {
    if (skill != null) {
      member2Skills.add(skill);
    }
  }
  
  // TODO: Write your code below
  // Use the union() method to combine both skill sets
  // Find overlapping skills between the two sets
  // Calculate total unique skills
  
  // Output the results in the required format
  print("Project: $projectName");
  print("Team Member 1 skills: $member1Skills");
  print("Team Member 2 skills: $member2Skills");
  // Print combined skills, total count, overlapping skills, and status
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow