Merge two maps
Updated
Merge combines two key sets. On conflict, pick a policy: last map wins, first map wins, or combine values (add counts, concat lists). Size of the result is |keysA ∪ keysB|.
What to focus on
Do not assume iteration order.
Worked examples
disjoint
a = [4] as {4:2}, b = [1] as {1:8}- no conflict
Result: {4:2, 1:8}
add counts
a = [4, 1] as {4:1,1:2}, b = [1, 9] as {1:1,9:1}- 1:2+1=3
Result: {4:1, 1:3, 9:1}
empty
a = [], b = [4] as {4:2}- copy b
Result: {4:2}
Try it
Write merge(a, b, policy). a and b are non-empty maps. policy is "union", "addCounts", or "lastWins". Return the merged map. Do not assume iteration order. a = [4] as {4:2}, b = [1] as {1:8} → {4:2, 1:8}. a = [4, 1] as {4:1,1:2}, b = [1, 9] as {1:1,9:1} → {4:1, 1:3, 9:1}. a = [], b = [4] as {4:2} → {4:2}.
keys are integers. Either map may be empty.