Using Dictionaries

Not Started

A dictionary maps keys to values with O(1) insert, lookup, and delete. Build the map in one pass, then query or compare.

When to Use

Counting occurrences: How many times does each element appear? e.g. character frequencies in a string
Tracking mappings: Does character A always map to character B? Use two maps to enforce one-to-one relationships (isomorphic strings)
Fast existence checks: Store obstacle positions or visited states for instant lookup
Complement lookups: Store what you've seen so far; check if the complement of the current element exists (two-sum pattern)

Create a Dictionary

freq = {}                        # empty dict
freq = {'a': 1, 'b': 2}         # literal syntax
unordered_map<char,int> freq;                // empty map
unordered_map<char,int> freq = {{'a',1}, {'b',2}};  // initializer list
var freq = new Dictionary<char,int>();          // empty dict
var freq = new Dictionary<char,int> {
    {'a', 1}, {'b', 2}
};  // collection initializer
Map<Character,Integer> freq = new HashMap<>();  // empty map

Insert / Update a Value

freq['a'] = 1                      # set or overwrite
freq['a'] = freq.get('a', 0) + 1  # increment (safe if missing)
freq['a'] = 1;    // set or overwrite
freq['a']++;      // auto-inits to 0 then increments
freq['a'] = 1;                                   // set or overwrite
freq['a'] = freq.GetValueOrDefault('a', 0) + 1;  // increment (safe)
freq.put('a', 1);                              // set or overwrite
freq.put('a', freq.getOrDefault('a', 0) + 1);  // increment (safe)

Lookup a Value

val = freq['a']              # KeyError if missing
val = freq.get('a', 0)       # returns 0 if missing
if 'a' in freq:             # check existence first
    val = freq['a']
int val = freq['a'];        // auto-inserts 0 if missing!
if (freq.count('a'))        // check existence first
    val = freq['a'];
int val = freq['a'];                     // throws if missing
int val = freq.GetValueOrDefault('a', 0); // returns 0 if missing
if (freq.ContainsKey('a'))              // check existence first
    val = freq['a'];
int val = freq.get('a');                  // null if missing
int val = freq.getOrDefault('a', 0);      // returns 0 if missing
if (freq.containsKey('a'))               // check existence first
    val = freq.get('a');

Delete a Key

del freq['a']       # KeyError if missing
freq.pop('a', None)  # safe delete, returns None if missing
freq.erase('a');  // no error if missing
freq.Remove('a');  // returns true if removed, false if not found
freq.remove('a');  // returns previous value or null

Loop Through a Dictionary

for key in freq:                # keys only
    print(key, freq[key])
for key, val in freq.items():   # key-value pairs
    print(key, val)
for (auto& [key, val] : freq)  // structured binding (C++17)
    cout << key << " " << val;
for (auto& p : freq)           // pair access
    cout << p.first << " " << p.second;
foreach (var kvp in freq)
    Console.WriteLine(kvp.Key + " " + kvp.Value);
for (var entry : freq.entrySet())
    System.out.println(entry.getKey() + " " + entry.getValue());

Get Size

len(freq)  # number of key-value pairs
freq.size();  // number of key-value pairs
freq.Count;  // number of key-value pairs (property)
freq.size();  // number of key-value pairs