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
EasyCreate a program that manages a data cleaning system by removing duplicate entries from survey responses. Your program should:
- Read a string input representing the survey title
- Read multiple string inputs representing survey responses (the input will end when you receive
"responses_done") - Create a List to store all the original responses (including duplicates)
- Use
Set.from()to convert the list into a Set, automatically removing duplicates - Use
toList()to convert the Set back into a List of unique responses - 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 successfullyIf 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 successfullyIf 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 successfullyYour 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
}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