Set Difference
Part of the Logic & Flow section of Coddy's Dart journey — lesson 24 of 65.
Sometimes you need to find elements that exist in one Set but not in another. The difference() method solves this by creating a new set containing only the elements from the first set that are not present in the second set.
Think of it as subtraction for sets - you're removing all elements of the second set from the first set. The difference() method returns a new set with the remaining elements, leaving both original sets unchanged.
Set<String> todaysTasks = {'email', 'meeting', 'report', 'calls'};
Set<String> completedTasks = {'email', 'calls'};
Set<String> remainingTasks = todaysTasks.difference(completedTasks);
print(remainingTasks); // {meeting, report}This operation is particularly useful for finding what's missing, what's left to do, or what's unique to one collection compared to another. Unlike intersection which finds commonalities, difference highlights what makes one set distinct from another.
Challenge
EasyCreate a program that manages a project management system by identifying tasks that are planned but not yet completed. Your program should:
- Read a string input representing the project name
- Read multiple string inputs representing planned tasks (the input will end when you receive
"planned_done") - Read multiple string inputs representing completed tasks (the input will end when you receive
"completed_done") - Create two separate Sets to store the planned tasks and completed tasks
- Use the
difference()method to find tasks that are planned but not yet completed - Print the project status analysis in the exact format shown below
For example, if the project name is "Website Launch", the planned tasks are "Design mockups", "Write content", "Setup database", "Test functionality", "Deploy site", and the completed tasks are "Design mockups", "Setup database", your program should output:
Project: Website Launch
Planned tasks: {Design mockups, Write content, Setup database, Test functionality, Deploy site}
Completed tasks: {Design mockups, Setup database}
Remaining tasks: {Write content, Test functionality, Deploy site}
Tasks completed: 2
Tasks remaining: 3
Progress: 40.0% complete
Status: Project in progress - 3 task(s) remainingIf the project name is "Mobile App Development", the planned tasks are "Create wireframes", "Develop UI", "Implement features", and the completed tasks are "Create wireframes", "Develop UI", "Implement features", your program should output:
Project: Mobile App Development
Planned tasks: {Create wireframes, Develop UI, Implement features}
Completed tasks: {Create wireframes, Develop UI, Implement features}
Remaining tasks: {}
Tasks completed: 3
Tasks remaining: 0
Progress: 100.0% complete
Status: Project completed successfullyIf the project name is "Marketing Campaign", the planned tasks are "Research target audience", "Create content", "Design graphics", "Launch campaign", "Analyze results", and the completed tasks are "Research target audience", your program should output:
Project: Marketing Campaign
Planned tasks: {Research target audience, Create content, Design graphics, Launch campaign, Analyze results}
Completed tasks: {Research target audience}
Remaining tasks: {Create content, Design graphics, Launch campaign, Analyze results}
Tasks completed: 1
Tasks remaining: 4
Progress: 20.0% complete
Status: Project in progress - 4 task(s) remainingYour program must use the difference() method to find tasks that exist in the planned tasks Set but not in the completed tasks Set. Calculate the progress percentage by dividing completed tasks by total planned tasks and multiplying by 100, rounded to 1 decimal place. If all tasks are completed, show "Project completed successfully" as the status, otherwise show "Project in progress - X task(s) remaining" where X is the number of remaining tasks.
Cheat sheet
The difference() method creates a new set containing elements from the first set that are not present in the second set:
Set<String> todaysTasks = {'email', 'meeting', 'report', 'calls'};
Set<String> completedTasks = {'email', 'calls'};
Set<String> remainingTasks = todaysTasks.difference(completedTasks);
print(remainingTasks); // {meeting, report}This operation is like subtraction for sets - it removes all elements of the second set from the first set, returning a new set with the remaining elements while leaving both original sets unchanged.
Try it yourself
import 'dart:io';
void main() {
// Read project name
String? projectName = stdin.readLineSync();
// Read planned tasks until "planned_done"
Set<String> plannedTasks = <String>{};
String? task;
while ((task = stdin.readLineSync()) != "planned_done") {
if (task != null) {
plannedTasks.add(task);
}
}
// Read completed tasks until "completed_done"
Set<String> completedTasks = <String>{};
while ((task = stdin.readLineSync()) != "completed_done") {
if (task != null) {
completedTasks.add(task);
}
}
// TODO: Write your code below
// Use the difference() method to find remaining tasks
// Calculate progress percentage
// Determine project status
// Output the results
print("Project: $projectName");
print("Planned tasks: $plannedTasks");
print("Completed tasks: $completedTasks");
// Add remaining output statements here
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced List Manipulation
List Properties: first & lastList State: isEmpty & isNotEmpReversing a ListAdding to a List: insertList Removal: removeWhereFinding in a List: indexOfSorting a ListShuffling a ListRecap - List Organizer4Advanced Map Manipulation
Iterating Over a MapChecking for Keys and ValuesMap Properties: keys & valuesConditional Add: putIfAbsentRemoving Entries from a MapNested MapsRecap - Inventory Update2Functional List Operations
Transforming with 'map'Filtering with 'where'Using '.toList()'Checking Conditions with 'any'Conditions with 'every'Finding with 'firstWhere'Recap - Data Filtering5Project: Shopping Cart Calc
Project SetupAdding Items to the Cart3Sets
What is a Set?Creating a SetAdding and Removing from SetsChecking for Elements in a SetConverting a List to a SetSet UnionSet IntersectionSet DifferenceRecap - Unique Guest List