Menu
Coddy logo textTech

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 Map

You 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 icon

Challenge

Easy

Create a program that manages a blog tagging system by creating a Set of unique tags for a blog post. Your program should:

  1. Read a string input representing the blog post title
  2. Read multiple string inputs representing tags to be added to the post (the input will end when you receive "done")
  3. Create a Set of strings to store the unique tags
  4. Add each tag to the Set (duplicates will be automatically removed)
  5. 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 successfully

If 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 successfully

If 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 successfully

Your 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 Map

Sets 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
}
quiz iconTest yourself

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

All lessons in Logic & Flow