using System;
using System.Collections.Generic;
class Program
{
// Simple phone book without hash map
static string FindNumberLinear(List<(string name, string number)> phoneBook, string target)
{
foreach (var entry in phoneBook)
{
if (entry.name == target)
return entry.number; // found target
}
return null; // not found
}
// Simple hash map simulation using Dictionary
static string FindNumberHashMap(Dictionary<string, string> phoneBook, string target)
{
if (phoneBook.ContainsKey(target))
return phoneBook[target]; // direct access
return null; // not found
}
static void Main()
{
var listPhoneBook = new List<(string, string)>
{
("Bob", "123"),
("Charlie", "456"),
("Alice", "789"),
("David", "101")
};
var hashPhoneBook = new Dictionary<string, string>
{
{"Bob", "123"},
{"Charlie", "456"},
{"Alice", "789"},
{"David", "101"}
};
string target = "Alice";
Console.WriteLine("Linear search result: " + FindNumberLinear(listPhoneBook, target));
Console.WriteLine("Hash map search result: " + FindNumberHashMap(hashPhoneBook, target));
}
}foreach (var entry in phoneBook)
{
if (entry.name == target)
return entry.number; // found target
}
Check each entry one by one until target is found
if (phoneBook.ContainsKey(target))
return phoneBook[target]; // direct access
Use hash map to jump directly to target's value
Linear search result: 789
Hash map search result: 789