Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function wordOccurrence(text) {
- let words = new Map();
- text.forEach(word => {
- if (!words.has(word)) {
- words.set(word, 1);
- } else {
- words.set(word, words.get(word) + 1);
- }
- });
- let wordOrder = Array.from(words.entries())
- .sort((a, b) => b[1] - (a[1]));
- wordOrder.forEach(entry => console.log(`${entry[0]} -> ${entry[1]}`));
- }
- OR
- function wordOccurrences(words) {
- const map = new Map();
- words.forEach(word => {
- if (map.has(word)) {
- map.set(word, map.get(word) + 1);
- } else {
- map.set(word, 1);
- }
- });
- const sortedMap = new Map([...map.entries()].sort((a, b) => b[1] - a[1]));
- sortedMap.forEach((count, word) => console.log(`${word} -> ${count} times`));
- }
Advertisement
Add Comment
Please, Sign In to add comment