Count Common Words With One Occurrence
Here’s a way to achieve the given task by defining the countWords
function:
|
|
Explanation
- This code uses the
Counter
class from thecollections
module to count the occurrences of each word inwords1
andwords2
. - It initializes a variable
result
to keep track of the number of strings that appear exactly once in both arrays. - Then, it iterates through the count of words in
words1
and checks if any word appears exactly once in bothwords1
andwords2
. If so, it increments the result by 1. - Finally, it returns the
result
, which is the number of strings that appear exactly once in each of the two arrays.