Creating a Set
Part of the Logic & Flow section of Coddy's Dart journey — lesson 18 of 65.
Now that you understand what a Set is, let's learn how to create one. In Dart, you create a Set using curly braces {} with the elements separated by commas, similar to how you might write a mathematical set.
Set<String> colors = {'red', 'blue', 'green'};
print(colors); // {red, blue, green}It's important not to confuse this syntax with creating a Map. While both use curly braces, a Set contains only values, whereas a Map contains key-value pairs separated by colons. An empty Set must be explicitly declared with a type to distinguish it from an empty Map.
Set<String> emptySet = <String>{}; // Empty Set
Map<String, int> emptyMap = {}; // Empty MapYou can also create a Set with duplicate values, but remember that duplicates will be automatically removed, leaving only unique elements in the final collection.
Challenge
EasyCreate a program that manages a blog tagging system by creating a Set of unique tags for a blog post. Your program should:
- Read a string input representing the blog post title
- Read multiple string inputs representing tags to be added to the post (the input will end when you receive
"done") - Create a Set of strings to store the unique tags
- Add each tag to the Set (duplicates will be automatically removed)
- Print the blog tagging results in the exact format shown below
For example, if the blog title is "Introduction to Dart Programming" and the tags are "programming", "dart", "tutorial", "programming", "beginner", your program should output:
Blog Post: Introduction to Dart Programming
Tags added: [programming, dart, tutorial, programming, beginner]
Unique tags: {programming, dart, tutorial, beginner}
Total unique tags: 4
Status: Blog post tagged successfullyIf the blog title is "Web Development Tips" and the tags are "web", "html", "css", "web", "html", "javascript", your program should output:
Blog Post: Web Development Tips
Tags added: [web, html, css, web, html, javascript]
Unique tags: {web, html, css, javascript}
Total unique tags: 4
Status: Blog post tagged successfullyIf the blog title is "Getting Started" and the tags are "guide", "basics", your program should output:
Blog Post: Getting Started
Tags added: [guide, basics]
Unique tags: {guide, basics}
Total unique tags: 2
Status: Blog post tagged successfullyYour program must create a Set using curly braces {} syntax and demonstrate how Sets automatically handle duplicate removal. Store all input tags in a list first to show what was originally entered, then create the Set to show the unique tags. The Set should display its contents in the standard Set format with curly braces.
Cheat sheet
Create a Set using curly braces {} with elements separated by commas:
Set<String> colors = {'red', 'blue', 'green'};
print(colors); // {red, blue, green}For empty Sets, explicitly declare the type to distinguish from empty Maps:
Set<String> emptySet = <String>{}; // Empty Set
Map<String, int> emptyMap = {}; // Empty MapSets automatically remove duplicate values, keeping only unique elements.
Try it yourself
import 'dart:io';
void main() {
// Read the blog post title
String? title = stdin.readLineSync();
// Read tags until "done" is entered
List<String> allTags = [];
String? tag;
while ((tag = stdin.readLineSync()) != "done") {
if (tag != null) {
allTags.add(tag);
}
}
// TODO: Write your code here
// Create a Set from the tags list to get unique tags
// Calculate the total number of unique tags
// Output the results
print("Blog Post: $title");
print("Tags added: $allTags");
// Print unique tags in Set format
// Print total unique tags count
// Print status message
}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