Guest User

Most Frequent word

a guest
Jul 19th, 2014
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. function findMostFreqWord(text) {
  2. var wordCount = {};
  3. text = text.replace(/,/g, "");
  4. var words = text.split(" ");
  5. for (var i in words) {
  6. var word = words[i];
  7. if (!wordCount[word]) {
  8. wordCount[word] = 0;
  9. }
  10. wordCount[word]++;
  11. }
  12. return wordCount;
  13. }
  14. var text = 'Welcome to SoftUni. Welcome to Java. Welcome everyone.';
  15. text = text.toLowerCase();
  16. var words = findMostFreqWord(text);
  17. var max = Number.MIN_VALUE;
  18. for (var i in words) {
  19. if (words[i] >= max) {
  20. max = words[i];
  21. console.log(i + ' -> ' + max + " times");
  22. }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment