Lesson 4
Applying Dictionaries in Real-World Scenarios
Introduction and Goal Setting

Hello there! In this lesson, we will apply Dictionaries to real-world challenges. Our focus will be on solving tasks such as cataloging books in a library, counting votes in an election, and tracking inventories.

Real-World Scenarios Calling for Dictionaries

Dictionaries are beneficial in real-life applications, such as the ones mentioned above, due to their ability to rapidly retrieve data with unique keys and efficiently handle larger datasets. Let's understand their efficiency with some actual examples.

Solving Real-World Task 1: Cataloging Books in a Library

Suppose you're asked to manage the cataloging of books in a library. Here, the book ID serves as the key, while the details of the book, such as the title, author, and year of publication, are stored as values.

This approach allows us to add, search for, and remove books from our library catalog using just a few lines of C# code.

C#
1using System; 2using System.Collections.Generic; 3 4class Solution { 5 public static void Main(string[] args) { 6 // Initializing a Dictionary 7 Dictionary<string, Dictionary<string, string>> libraryCatalog = new Dictionary<string, Dictionary<string, string>>(); 8 9 // Details of a book 10 string bookId = "123"; 11 // Creating a Dictionary to store details of the book 12 var bookDetails = new Dictionary<string, string>(); 13 bookDetails.Add("title", "To Kill a Mockingbird"); 14 bookDetails.Add("author", "Harper Lee"); 15 bookDetails.Add("year_published", "1960"); 16 17 libraryCatalog.Add(bookId, bookDetails); // Adding a book to library catalog, where value itself is a Dictionary 18 19 // Searching for a book 20 if (libraryCatalog.ContainsKey(bookId)) { 21 Console.WriteLine($"Title: {libraryCatalog[bookId]["title"]}, Author: {libraryCatalog[bookId]["author"]}, Year Published: {libraryCatalog[bookId]["year_published"]}"); 22 } 23 24 // Removing a book from the library 25 libraryCatalog.Remove(bookId); 26 } 27}

As you can see, Dictionaries make the task of cataloging books in the library simpler and more efficient!

Solving Real-World Task 2: Counting Votes in an Election

Imagine a scenario in which we need to count votes in an election. We employ a Dictionary, where each name is a unique key, and the frequency of that name serves as the associated value. Let's write some C# code to better understand this.

C#
1using System; 2using System.Collections.Generic; 3 4class Solution { 5 public static void Main(string[] args) { 6 // Cast votes 7 var votesList = new List<string> { "Alice", "Bob", "Alice", "Charlie", "Bob", "Alice" }; 8 // Initializing a Dictionary 9 var voteCounts = new Dictionary<string, int>(); 10 11 // Counting the votes 12 foreach (var name in votesList) { 13 voteCounts[name] = voteCounts.GetValueOrDefault(name, 0) + 1; 14 } 15 16 foreach (var entry in voteCounts) { 17 Console.WriteLine($"{entry.Key}: {entry.Value}"); 18 } 19 // Prints: Alice: 3, Bob: 2, Charlie: 1 20 } 21}

Dictionaries facilitate the efficient counting of votes.

Solving Real-World Task 3: Tracking Store Inventories

Finally, consider a task that involves managing a store's inventory. Here, we can use a Dictionary in which product names are keys and quantities are values. This approach allows us to easily add new items, adjust the quantity of items, check whether an item is in stock, and much more.

C#
1using System; 2using System.Collections.Generic; 3 4class Solution { 5 public static void Main(string[] args) { 6 // Initializing an inventory 7 var storeInventory = new Dictionary<string, int>(); 8 storeInventory.Add("Apples", 100); 9 storeInventory.Add("Bananas", 80); 10 storeInventory.Add("Oranges", 90); 11 12 // Adding a new product to inventory and setting its quantity 13 storeInventory["Pears"] = 50; 14 15 // Updating the number of apples in inventory 16 if (storeInventory.ContainsKey("Apples")) { 17 storeInventory["Apples"] += 20; 18 } 19 20 // A product to be checked 21 string prod = "Apples"; 22 Console.WriteLine($"Total {prod} in stock: {storeInventory[prod]}"); 23 24 // Check if a product is in stock 25 prod = "Mangoes"; 26 if (storeInventory.ContainsKey(prod)) { 27 Console.WriteLine($"{prod} are in stock."); // If mangoes exist in inventory 28 } else { 29 Console.WriteLine($"{prod} are out of stock."); // If mangoes don't exist in inventory 30 } 31 } 32}

Thus, when managing inventory data, Dictionaries offer an efficient solution!

Lesson Summary and Practice

In this lesson, we bridged the gap between the theory of Dictionaries and their practical applications. We explored real-world problems that can be solved using Dictionaries and implemented C# code to address them.

Now, get ready for hands-on practice exercises that will help reinforce these concepts and hone your Dictionary problem-solving skills. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.