Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function makeOccurrenceCounter(arr) {
- const occDict = {};
- for(let item of arr) {
- if(occDict[item]) {
- occDict[item]++;
- } else {
- occDict[item] = 1;
- }
- }
- return occDict;
- }
- function common(arr1, arr2, arr3) {
- const [occ1, occ2, occ3] = [
- makeOccurrenceCounter(arr1),
- makeOccurrenceCounter(arr2),
- makeOccurrenceCounter(arr3),
- ];
- const minOcc = {};
- const merged = [...arr1, ...arr2, ...arr3];
- for(let item of merged) {
- minOcc[item] = Math.min(
- occ1[item] || 0,
- occ2[item] || 0,
- occ3[item] || 0,
- )
- }
- return minOcc;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement