Write a function `wordFrequency(str)` that returns a Map with word frequencies. Convert to a plain object for output.
Problem Statement
Write a function `wordFrequency(str)` that returns a Map with word frequencies. Convert to a plain object for output.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
Split the string by spaces to get words.
š” Hint 2:
Use a Map to store word counts. Map.get() and Map.set() for access.
š” Hint 3:
Convert Map to object: Object.fromEntries(map).
ā
Solution:
```javascript
function wordFrequency(str) {
const map = new Map();
str.split(" ").forEach(word => {
map.set(word, (map.get(word) || 0) + 1);
});
return Object.fromEntries(map);
}
```