I have two hashmaps. This is just example of 2 hashmaps, there can be n hashmaps.
They look like this
HashMap A = [(a, 23),(b,25),(c,43),(d,34)]
HashMap B = [(a, 32),(b,52),(d,55)]
Now I want to compare these hashmaps in such a way so that I can put the missing key of ‘c’ into HashMap B with value as 0.
How can I do that? Remember there can be n HashMaps.
Let’s call the “target” HashMap the one that will get the missing keys and “sources” each of the others. For each key in each source, if the target does not contain the key then associate the zero with that key in the target:
Now if you want to ensure that all maps have all keys from all other maps then you should first compute the entire set of keys and add the missing ones to each map:
Both solutions perform at O(n*k) where
nis the number of maps andkis the average number of keys in each map.