C#
Array
Fill With Constant
Note
Unlike C/C++, C# sets a default value on primitive types (eg: int[] -> everything is set to 0 by default)
Sort One Array Based on Another
Dictionary (HashMap)
// Initialize
Dictionary<KeyType, ValueType> hashmap = [];
// Add key value pair
hashmap.TryAdd(key, value);
// Discard key value pair
hashmap.Remove(key, out T value) // Optional overload to capture value
// Check if key exists in dictionary
hashmap.ContainsKey(key)
// Get value associated with a key
hashmap.TryGetValue(key, out T value)
// Iterate over key value pairs
foreach(KeyValuePair<T1, T2> kvp in hashmap) {
Console.WriteLine($"{kvp.key}: {kvp.value}");
}
// Separate iterable collection of keys and values
ICollection<KeyType> = hashmap.Keys;
ICollection<ValueType> = hashmap.Values;
Obtain Maximum/Minimum
var maxKvpByKey = hashmap.MaxBy(kvp => kvp.Key);
var maxKvpByValue = hashmap.MaxBy(kvp => kvp.Value);
var minKvpByKey = hashmap.MinBy(kvp => kvp.Key);
var minKvpByValue = hashmap.MinBy(kvp => kvp.Value);
Using bool Output of Operations
Operations like TryAdd emit a bool success output. It can be optionally captured to check if the operation was completed.