Recap - HashMap
Part of the Logic & Flow section of Coddy's C# journey — lesson 51 of 66.
Challenge
EasyCreate a method called ProcessDictionary that takes a Dictionary<string, int> and an array of operations as parameters. The method should perform these operations:
- If the operation is "GET key", print the value for that key or "Not found" if the key doesn't exist
- If the operation is "CHECK key", print "Exists" if the key exists, otherwise print "Not found"
- If the operation is "MODIFY key value", do the following:
- If the key exists and has the same value as provided, increase its value by 1
- If the key exists but has a different value, remove the key
- If the key doesn't exist, add it with the provided value
Return the updated Dictionary after processing all operations.
Try it yourself
using System;
using System.Collections.Generic;
class Program
{
public static Dictionary<string, int> ProcessDictionary(Dictionary<string, int> data, string[] operations)
{
// Write your code here
}
static void Main(string[] args)
{
// Initialize a dictionary with some data
Dictionary<string, int> data = new Dictionary<string, int>
{
{ "Apple", 10 },
{ "Banana", 5 },
{ "Orange", 7 }
};
// Sample operations
string[] operations = new string[]
{
"GET Apple",
"CHECK Mango",
"MODIFY Banana 5",
"MODIFY Orange 8",
"MODIFY Mango 3"
};
Dictionary<string, int> result = ProcessDictionary(data, operations);
// Display the result
Console.WriteLine("Updated Dictionary:");
foreach (var item in result)
{
Console.WriteLine($"{item.Key}: {item.Value}");
}
}
}All lessons in Logic & Flow
1Multi-dimensional Arrays
2D Arrays BasicsDeclaring and Initializing 2DAccessing 2D Array ElementsNested Loops with 2D ArraysJagged ArraysCommon Matrix OperationsRecap - Multi-dimensional4Flow Control Techniques
Early ReturnsGuard ClausesJump Statements (goto)Break and ContinueFlatten Nested Conditionals7Logical Operators Advanced
Short-Circuit EvaluationConditional Logical OperatorsOperator PrecedenceRecap - Advanced Operators2Advanced Decision Making
Multiple ConditionsComplex Boolean LogicIf vs. Switch ComparisonNested Switch StatementsRecap - Advanced Decisions5Exception Handling
Try-Catch BasicsException TypesMultiple Catch BlocksWorking with FilesFinally BlockUsing vs. Try-FinallyCustom ExceptionsRecap - Error Handling3Loop Enhancements
Loop PerformanceIterating ComplexEach Loop TypeRefactoring LoopsRecap - Optimized Loops6Null Handling
Null Reference BasicsNullable Value TypesNull Checking PatternsDefensive ProgrammingRecap - Null Safety9HashMap Part 1
What is a HashMap?Declare a HashMapCheck If Key ExistsAccessing ValuesModifying DictionariesRecap - HashMap