A dictionary maps keys to values with O(1) insert, lookup, and delete. Build the map in one pass, then query or compare.
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
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)
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');
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
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());
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