Menu
Coddy logo textTech

Converting a List to a Set

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

One of the most practical applications of Set is removing duplicate elements from a List. Since sets automatically eliminate duplicates, you can easily convert a list with repeated values into a collection of unique elements.

To convert a List to a Set, you simply pass the list to the Set.from() constructor. This creates a new set containing all the unique elements from the original list.

List<int> numbersWithDuplicates = [1, 2, 2, 3, 3, 3, 4, 5, 5];
Set<int> uniqueNumbers = Set.from(numbersWithDuplicates);

print(uniqueNumbers);  // {1, 2, 3, 4, 5}

If you need the result back as a List (perhaps to maintain compatibility with other parts of your code), you can convert the set back to a list using toList(). This gives you a list containing only the unique elements from the original.

List<int> uniqueList = Set.from(numbersWithDuplicates).toList();
print(uniqueList);  // [1, 2, 3, 4, 5]

This pattern of List → Set → List is extremely common when you need to clean up data by removing duplicates while preserving the list structure for further processing.

challenge icon

Challenge

Easy

Create a program that manages a data cleaning system by removing duplicate entries from survey responses. Your program should:

  1. Read a string input representing the survey title
  2. Read multiple string inputs representing survey responses (the input will end when you receive "responses_done")
  3. Create a List to store all the original responses (including duplicates)
  4. Use Set.from() to convert the list into a Set, automatically removing duplicates
  5. Use toList() to convert the Set back into a List of unique responses
  6. Print the data cleaning results in the exact format shown below

For example, if the survey title is "Customer Satisfaction Survey" and the responses are "Excellent", "Good", "Excellent", "Fair", "Good", "Excellent", "Poor", your program should output:

Survey: Customer Satisfaction Survey
Original responses: [Excellent, Good, Excellent, Fair, Good, Excellent, Poor]
Total responses collected: 7
Converting to Set to remove duplicates...
Unique responses (Set): {Excellent, Good, Fair, Poor}
Converting back to List...
Cleaned responses: [Excellent, Good, Fair, Poor]
Duplicates removed: 3
Final unique responses: 4
Status: Data cleaning completed successfully

If the survey title is "Product Feedback" and the responses are "Like", "Dislike", "Like", "Like", your program should output:

Survey: Product Feedback
Original responses: [Like, Dislike, Like, Like]
Total responses collected: 4
Converting to Set to remove duplicates...
Unique responses (Set): {Like, Dislike}
Converting back to List...
Cleaned responses: [Like, Dislike]
Duplicates removed: 2
Final unique responses: 2
Status: Data cleaning completed successfully

If the survey title is "Event Rating" and the responses are "Amazing", "Okay", "Great", your program should output:

Survey: Event Rating
Original responses: [Amazing, Okay, Great]
Total responses collected: 3
Converting to Set to remove duplicates...
Unique responses (Set): {Amazing, Okay, Great}
Converting back to List...
Cleaned responses: [Amazing, Okay, Great]
Duplicates removed: 0
Final unique responses: 3
Status: Data cleaning completed successfully

Your program must demonstrate the complete List → Set → List pattern. First store all responses in a List, then use Set.from() to create a Set from the List, and finally use toList() to convert the Set back to a List. Calculate the number of duplicates removed by subtracting the final unique count from the original total count.

Cheat sheet

To remove duplicates from a List, convert it to a Set using Set.from():

List<int> numbersWithDuplicates = [1, 2, 2, 3, 3, 3, 4, 5, 5];
Set<int> uniqueNumbers = Set.from(numbersWithDuplicates);
print(uniqueNumbers);  // {1, 2, 3, 4, 5}

To convert a Set back to a List, use toList():

List<int> uniqueList = Set.from(numbersWithDuplicates).toList();
print(uniqueList);  // [1, 2, 3, 4, 5]

The List → Set → List pattern is commonly used for data cleaning while preserving list structure.

Try it yourself

import 'dart:io';

void main() {
  // Read survey title
  String? surveyTitle = stdin.readLineSync();
  
  // Read survey responses until "responses_done"
  List<String> originalResponses = [];
  String? response;
  while ((response = stdin.readLineSync()) != "responses_done") {
    if (response != null) {
      originalResponses.add(response);
    }
  }
  
  // TODO: Write your code below
  // 1. Use Set.from() to convert the list to a Set (removes duplicates)
  // 2. Use toList() to convert the Set back to a List
  // 3. Calculate duplicates removed and final counts
  
  // Print the results in the required format
  print("Survey: $surveyTitle");
  print("Original responses: $originalResponses");
  // Add remaining print statements here
}
quiz iconTest yourself

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

All lessons in Logic & Flow