Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a dictionary to store character counts.
DSA C#
var charCounts = new Dictionary<char, [1]>();Attempts:
3 left
💡 Hint
Common Mistakes
Using string or char as the value type instead of int.
Using bool which is not suitable for counting.
✗ Incorrect
We use int as the value type to count how many times each character appears.
2fill in blank
mediumComplete the code to check if the dictionary already contains the character key.
DSA C#
if (!charCounts.[1](c)) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using Add which inserts a new key but does not check existence.
Using Remove or Clear which modify the dictionary.
✗ Incorrect
ContainsKey checks if the dictionary has the given character as a key.
3fill in blank
hardFix the error in incrementing the count of a character in the dictionary.
DSA C#
charCounts[c] [1] 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-=' which decreases the count.
Using '*=' or '/=' which multiply or divide the count.
✗ Incorrect
The '+=' operator increases the count by 1 for the character key.
4fill in blank
hardFill both blanks to add a new character with count 1 if it does not exist.
DSA C#
if (!charCounts.ContainsKey(c)) { charCounts.[1](c, [2]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Remove instead of Add.
Setting initial count to 0 which means no occurrence.
✗ Incorrect
Add inserts the character with initial count 1 when it is not already in the dictionary.
5fill in blank
hardFill all three blanks to create a dictionary with character counts for letters only.
DSA C#
var filteredCounts = charCounts.Where(kv => char.IsLetter(kv.Key))
.ToDictionary(kv => [1], kv => [2] + [3]);Attempts:
3 left
💡 Hint
Common Mistakes
Using kv.Key.ToString() as key which changes the type.
Not adding 1 to the value or using wrong operators.
✗ Incorrect
We keep the character key as is, and increase the count by 1 for demonstration.
