In this exercise, I created one HashMap by thread, and when all threads were done, I merged all the HashMap within the first HashMap .reduce
as follow
join_handles
.into_iter()
.map(|handle| handle.join().unwrap_or(HashMap::new()))
.reduce(|mut global, local| {
local.into_iter().for_each(|(key, value)| {
global.entry(key).and_modify(|x| *x += value);
});
global
})
Notice that I used and_modify
without using or_default
or or_insert
.
This oversight was not caught by the test suite, because it just happened that the first HashMap was already containing all the possible char.
My code shouldn’t have been validated and one way to prevent it would be to simply add a test case with each slice of &str having different char.