Terminology (4)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Key equivalence
- The container’s rule for deciding whether two supplied keys identify one entry.
- Hash collision
- Different keys producing the same hash result; equality must still distinguish nonequivalent keys.
- Presence
- Whether an entry exists, independently of its mapped value.
- Default insertion
- Creating a missing entry while obtaining its default mapped value.
Keys identify entries
A map associates one value with each distinct key under its key-equivalence rule. The same key can be used to find or update an entry without knowing its position. Presence is a separate fact from the stored value: zero can be meaningful data.
Hash-based maps use a hash to narrow the search and equality to distinguish keys that share a hash. Equal keys must hash consistently; unequal keys may collide. Native operations also differ in whether they replace, insert or only observe, so inspect the operation as well as the key.
01Insertion and replacement
An insertion API may preserve or replace an existing value
A stocktake corrects a recorded quantity. After the update, operators need to distinguish an item recorded at zero from an unknown code before requesting a stock reassignment. The inventory tool prints its complete entries after each step, so an unchanged total cannot hide newly created records.
A counted quantity replaces the old record rather than adding another delivery. This reduced correction probe starts with code 1, updates its count and records a second code at zero. Predict the resulting entry count and stock for code 1.
const stocks = new Map<number, number>([[1, 3]]);
stocks.set(1, 8);
stocks.set(2, 0);
console.log("entries", stocks.size, "stock", stocks.get(1));
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts
Save one complete example at a time in a fresh folder under the selected filename. Run the shown checking/build command before execution. The browser lab supplies its editor, stdin fixtures and native runner.
02Key equivalence and collisions
Equal fields do not always mean equal keys
The stocktake may identify a record by zone and item code. Reconstructing that pair for a correction should identify the intended record under the chosen key representation. This probe inserts two equal-looking pairs and a different one; inspect the native key type before predicting which values remain.
const first = { zone: 1, code: 2 };
const sameFields = { zone: 1, code: 2 };
const other = { zone: 1, code: 3 };
const stocks = new Map([
[first, 7],
[sameFields, 9],
[other, 5]
]);
console.log("entries", stocks.size, "first", stocks.get(first));
03Presence and lookup effects
A default value does not establish membership
The operator next asks for a stock report. Code 1 is recorded at zero, while code 9 has no record. This reduced probe requests a default count for code 9; predict both its answer and the inventory size after the access.
const stocks = new Map([[1, 0]]);
const value = stocks.get(9) ?? 0;
console.log("value", value, "entries", stocks.size);
04Two arguments and one entry
Copied values do not establish independent locations
The reassignment tool accepts source and destination codes. A request may name the same stock record twice, so two local count variables need not represent independent entries. This reduced probe caches both counts before its writes. Predict the final stored count.
const stocks = new Map([[1, 3]]);
const from = 1;
const to = 1;
const source = stocks.get(from)!;
const target = stocks.get(to)!;
stocks.set(from, source - 1);
stocks.set(to, target + 1);
console.log("stock", stocks.get(1));
05Practice
Repair the inventory map
Input is set key, set value, probe key, source key, destination key, amount, entry count, then unique key/value pairs. Codes and counts are nonnegative small integers. Input decoding and sorted reporting are supplied.
A present zero remains an inventory entry. A move does nothing for zero amount or equivalent keys, and changes nothing when its source is missing or insufficient. A successful move may create the destination and keeps the source entry even at zero.
Start the lab
Replace a stock value, observe presence without insertion, then move stock while preserving key identity and rejected-path state.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?