Intersect arrays keeping each value's minimum count
Each warehouse exports its stock as a list of SKUs, with one entry per unit: ['mug', 'mug', 'cap'] means two mugs and one cap. To plan bundles that every warehouse can fulfil, the team needs the units that are available everywhere. A plain set intersection is not enough: if one warehouse has three mugs and another has two, only two mugs are available in both.
Implement intersectCounts(...arrays), which returns the multiset intersection of the arrays.
intersectCounts(...arrays) // => Array
Map, Set and includes use: NaN matches NaN, 0 matches -0, 1 does not match '1', and objects match only themselves.[].intersectCounts(
['mug', 'mug', 'cap', 'mug', 'tee'],
['cap', 'mug', 'mug', 'sock'],
['mug', 'cap', 'cap', 'mug', 'mug'],
);
// => ['mug', 'mug', 'cap']
intersectCounts([3, 1, 3, 2, 3], [3, 3, 1]); // => [3, 1, 3]
intersectCounts([1, '1', 2], ['1', 2, 2]); // => ['1', 2]
intersectCounts([NaN, 0, NaN], [-0, NaN]); // => [NaN, 0]
intersectCounts(['a', 'b'], []); // => []
intersectCounts(['a', 'a']); // => ['a', 'a']
intersectCounts(); // => []