-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay88.js
More file actions
28 lines (22 loc) · 767 Bytes
/
Day88.js
File metadata and controls
28 lines (22 loc) · 767 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//* Count Common Words Occurring Once in Two Arrays
const countCommonWords = (arr1, arr2) => {
const wordCount = {};
// Count words in arr1
for (let word of arr1) {
wordCount[word] = (wordCount[word] || 0) + 1;
}
// Count words in arr2 and filter common words occurring once
const commonWords = arr2.filter(word => {
if (wordCount[word] === 1) {
delete wordCount[word];
return true;
}
return false;
});
return commonWords.length;
};
// Example usage
const array1 = ['apple', 'banana', 'orange', 'apple'];
const array2 = ['apple', 'banana', 'grape', 'orange'];
const commonWordCount = countCommonWords(array1, array2);
console.log(commonWordCount); // Output: 2