Menu
Coddy logo textTech

Recap - HashMap

Part of the Logic & Flow section of Coddy's C# journey — lesson 51 of 66.

challenge icon

Challenge

Easy

Create a method called ProcessDictionary that takes a Dictionary<string, int> and an array of operations as parameters. The method should perform these operations:

  1. If the operation is "GET key", print the value for that key or "Not found" if the key doesn't exist
  2. If the operation is "CHECK key", print "Exists" if the key exists, otherwise print "Not found"
  3. 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