Counting Unique Words
Part of the Logic & Flow section of Coddy's Dart journey — lesson 55 of 65.
Challenge
EasyContinuing your text analyzer project, you'll now implement the unique word counting functionality. This step will determine how many distinct words appear in your text by using a Set to automatically remove duplicates from your word list.
- Use the
textToAnalyzevariable from your previous setup - Use the
totalCharactersvariable from your character counting step - Use the
wordListvariable from your word splitting step - Use the
totalWordsvariable from your word counting step - Convert the word list to a Set using
Set.from()to automatically remove duplicate words - Store the resulting Set in a variable called
uniqueWordsSet - Count the number of unique words by using the
.lengthproperty on the Set - Store the unique word count in a variable called
uniqueWordCount - Display the unique word analysis results in the exact format shown below
- Show the original text, character count, word list, total words, unique words set, and unique word count with proper formatting
For example, if your text analyzer was set up with "Hello world hello", your program should output:
Text Analysis Tool - Unique Words
====================================
Analyzing text: "Hello world hello"
Total characters: 17
Words found: [Hello, world, hello]
Total words: 3
====================================
Unique Word Analysis Results:
Unique words: {Hello, world, hello}
Unique word count: 3
====================================
Unique word analysis completed successfully.If your text analyzer was set up with "The cat and the dog", your program should output:
Text Analysis Tool - Unique Words
====================================
Analyzing text: "The cat and the dog"
Total characters: 19
Words found: [The, cat, and, the, dog]
Total words: 5
====================================
Unique Word Analysis Results:
Unique words: {The, cat, and, the, dog}
Unique word count: 4
====================================
Unique word analysis completed successfully.If your text analyzer was set up with "apple banana apple cherry banana", your program should output:
Text Analysis Tool - Unique Words
====================================
Analyzing text: "apple banana apple cherry banana"
Total characters: 33
Words found: [apple, banana, apple, cherry, banana]
Total words: 5
====================================
Unique Word Analysis Results:
Unique words: {apple, banana, cherry}
Unique word count: 3
====================================
Unique word analysis completed successfully.Your program should use Set.from() to convert the word list into a Set, which automatically removes duplicate words while preserving the unique ones. The .length property on the Set will give you the count of unique words. This analysis will help identify the vocabulary richness of the text and will be used alongside other metrics in the upcoming lessons.
Try it yourself
import 'dart:io';
void main() {
String? textToAnalyze = stdin.readLineSync();
int totalCharacters = textToAnalyze!.length;
List<String> wordList = textToAnalyze.split(' ');
int totalWords = wordList.length;
print('Text Analysis Tool - Word Count');
print('====================================');
print('Analyzing text: "$textToAnalyze"');
print('Total characters: $totalCharacters');
print('Words found: $wordList');
print('====================================');
print('Word Count Results:');
print('Total words: $totalWords');
print('====================================');
print('Word counting completed successfully.');
}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 Cart8Project: Simple Text Analyzer
Project Overview and SetupCounting Characters3Sets
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