Menu
Coddy logo textTech

Counting Unique Words

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

challenge icon

Challenge

Easy

Continuing 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.

  1. Use the textToAnalyze variable from your previous setup
  2. Use the totalCharacters variable from your character counting step
  3. Use the wordList variable from your word splitting step
  4. Use the totalWords variable from your word counting step
  5. Convert the word list to a Set using Set.from() to automatically remove duplicate words
  6. Store the resulting Set in a variable called uniqueWordsSet
  7. Count the number of unique words by using the .length property on the Set
  8. Store the unique word count in a variable called uniqueWordCount
  9. Display the unique word analysis results in the exact format shown below
  10. 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