- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
? Dictionary in C# — The Ultimate Tool for Fast Lookup
A Dictionary<TKey, TValue> is a powerful data structure that maps unique keys to values, allowing fast O(1) lookups and efficient data grouping.
When to Use Dictionary
Declaring and Initializing
// Declare and initialize
Dictionary<string, int> ageMap = new Dictionary<string, int>();
// Adding entries
ageMap["Alice"] = 25;
ageMap["Bob"] = 30;
? Common Operations
// Add or update
ageMap["Charlie"] = 28;
// Check key existence
if (ageMap.ContainsKey("Alice"))
Console.WriteLine(ageMap["Alice"]);
// Remove entry
ageMap.Remove("Bob");
// Loop through dictionary
foreach (var kvp in ageMap)
{
Console.WriteLine($"{kvp.Key} => {kvp.Value}");
}
? Interview Example 1: Two Sum
public int[] TwoSum(int[] nums, int target)
{
var map = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)
{
int complement = target - nums;
if (map.ContainsKey(complement))
return new int[] { map[complement], i };
map[nums] = i;
}
return new int[] { -1, -1 };
}
? Interview Example 2: Group Anagrams
public IList<IList<string>> GroupAnagrams(string[] strs)
{
var map = new Dictionary<string, List<string>>();
foreach (var word in strs)
{
char[] chars = word.ToCharArray();
Array.Sort(chars);
string key = new string(chars);
if (!map.ContainsKey(key))
map[key] = new List<string>();
map[key].Add(word);
}
return new List<IList<string>>(map.Values);
}
? Summary
Next up: Stack — ideal for solving nested and reverse order problems like Valid Parentheses and backtracking tasks.
A Dictionary<TKey, TValue> is a powerful data structure that maps unique keys to values, allowing fast O(1) lookups and efficient data grouping.
| Use Case | Why Use Dictionary |
|---|---|
| Map key to value | Quick access with known key |
| Frequency count | Count how many times items appear |
| Grouping elements | Categorize data using keys |
| Implement cache or memoization | Fast storage and retrieval |
// Declare and initialize
Dictionary<string, int> ageMap = new Dictionary<string, int>();
// Adding entries
ageMap["Alice"] = 25;
ageMap["Bob"] = 30;
? Common Operations
// Add or update
ageMap["Charlie"] = 28;
// Check key existence
if (ageMap.ContainsKey("Alice"))
Console.WriteLine(ageMap["Alice"]);
// Remove entry
ageMap.Remove("Bob");
// Loop through dictionary
foreach (var kvp in ageMap)
{
Console.WriteLine($"{kvp.Key} => {kvp.Value}");
}
? Interview Example 1: Two Sum
public int[] TwoSum(int[] nums, int target)
{
var map = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)
{
int complement = target - nums;
if (map.ContainsKey(complement))
return new int[] { map[complement], i };
map[nums] = i;
}
return new int[] { -1, -1 };
}
? Interview Example 2: Group Anagrams
public IList<IList<string>> GroupAnagrams(string[] strs)
{
var map = new Dictionary<string, List<string>>();
foreach (var word in strs)
{
char[] chars = word.ToCharArray();
Array.Sort(chars);
string key = new string(chars);
if (!map.ContainsKey(key))
map[key] = new List<string>();
map[key].Add(word);
}
return new List<IList<string>>(map.Values);
}
? Summary
| Feature | Syntax Example |
|---|---|
| Declare | new Dictionary<TKey, TValue>() |
| Add/Update | dict[key] = value; |
| Exists Check | dict.ContainsKey(key) |
| Remove | dict.Remove(key) |
| Iterate | foreach (var kvp in dict) |
Next up: Stack — ideal for solving nested and reverse order problems like Valid Parentheses and backtracking tasks.