📖 Scenario: Imagine you are analyzing a short message to find out how many times each letter appears. This is useful in text analysis, games, or coding puzzles.
🎯 Goal: You will build a program that counts how many times each character appears in a given string.
📋 What You'll Learn
Create a string variable with a specific message
Create an empty object to store character counts
Use a loop to count each character's frequency
Print the final character frequency object
💡 Why This Matters
🌍 Real World
Counting character frequency helps in text analysis, cryptography, and building word games.
💼 Career
Understanding how to count and store frequencies is useful in data processing, software development, and algorithm design.
Progress0 / 4 steps
1
Create the message string
Create a string variable called message and set it to the exact value "hello world".
DSA Typescript
Hint
Use const message = "hello world"; to create the string.
2
Create an empty object for counts
Create an empty object called charCounts to store the frequency of each character.
DSA Typescript
Hint
Use const charCounts: { [key: string]: number } = {}; to create an empty object.
3
Count each character frequency
Use a for loop with variable char to iterate over each character in message. Inside the loop, increase the count of char in charCounts by 1, or set it to 1 if it does not exist yet.
DSA Typescript
Hint
Use for (const char of message) and update counts inside the loop.
4
Print the character frequency object
Write a console.log statement to print the charCounts object.
DSA Typescript
Hint
Use console.log(charCounts); to show the result.
Practice
(1/5)
1. What does character frequency counting in a string help us find?
easy
A. How many times each character appears in the string
B. The length of the string
C. The first character of the string
D. The position of a character in the string
Solution
Step 1: Understand character frequency counting
Character frequency counting means counting how many times each character appears in a string.
Step 2: Identify what it helps find
This counting helps us know the number of occurrences of each character, not the length or position.
Final Answer:
How many times each character appears in the string -> Option A
Quick Check:
Frequency counting = character counts [OK]
Hint: Frequency counting means counting each character's occurrences [OK]
Common Mistakes:
Confusing frequency with string length
Thinking it finds character positions
Mixing frequency with first character only
2. Which TypeScript code snippet correctly initializes an empty object to store character frequencies?
easy
A. const freq = new Map();
B. const freq = {};
C. const freq = [];
D. const freq = '';
Solution
Step 1: Identify data structure for frequency counting
We need an object to store keys (characters) and values (counts). An empty object is initialized with {}.
Step 2: Check each option
const freq = new Map(); uses Map which is valid but not the simplest empty object. const freq = {}; correctly uses {}. const freq = []; is an array, not suitable. const freq = ''; is a string, not suitable.
Final Answer:
const freq = {}; -> Option B
Quick Check:
Empty object = {} [OK]
Hint: Use {} to start an empty object for frequency counts [OK]
B. freq should be initialized as an array, not object
C. Missing declaration of variable ch
D. Loop condition should be i < str.length, not i <= str.length
Solution
Step 1: Check loop bounds
Loop runs from i = 0 to i <= str.length, which means i goes up to str.length (3). But string indices go from 0 to length-1 (2).
Step 2: Identify out-of-bounds access
At i = 3, str[3] is undefined, causing freq[undefined] to be updated, which is incorrect.
Final Answer:
Loop condition should be i < str.length, not i <= str.length -> Option D
Quick Check:
Loop index must be less than length [OK]
Hint: Use i < length to avoid out-of-bounds errors [OK]
Common Mistakes:
Using <= instead of < in loop condition
Initializing freq as array instead of object
Confusing for...in with for...of loops
5. Given the string "aabbc", which TypeScript code snippet correctly returns an object with only characters that appear exactly twice?
hard
A. const freq: {[key: string]: number} = {};
for (const ch of str) {
freq[ch] = (freq[ch] || 0) + 1;
}
const result = Object.fromEntries(Object.entries(freq).filter(([k,v]) => v === 2));
B. const freq = new Map();
for (const ch of str) {
freq.set(ch, (freq.get(ch) || 0) + 1);
}
const result = Object.fromEntries([...freq].filter(([k,v]) => v === 1));
C. const freq = {};
for (const ch of str) {
freq[ch] = 2;
}
const result = freq;
D. const freq = {};
for (const ch of str) {
freq[ch] = (freq[ch] || 0) + 1;
}
const result = Object.keys(freq);
Solution
Step 1: Count frequencies correctly
Loop through string, count each character's occurrences in freq object.
Step 2: Filter characters with count exactly 2
Use Object.entries(freq) to get pairs, filter those with value 2, then convert back to object.
Final Answer:
Code snippet that counts and filters for count === 2 -> Option A
Quick Check:
Filter freq entries where value is 2 [OK]
Hint: Filter entries with value === 2 after counting [OK]
Common Mistakes:
Filtering for count === 1 instead of 2
Assigning fixed count 2 inside loop
Returning keys instead of filtered object
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.