Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function findMostFreqWord(value) {
- value = value.toLowerCase();
- var allWords = value.match(/\w+/gi);
- var words = {};
- for (var i = 0; i < allWords.length; i += 1) {
- if (allWords[i] in words) {
- words[allWords[i]] += 1;
- }
- else {
- words[allWords[i]] = 1;
- }
- }
- var keysSorted = Object.keys(words).
- sort(function(a,b){
- var compare = words[b]-words[a]
- return compare !== 0 ? compare : b < a});
- var max = words[keysSorted[0]];
- var sequence = 1;
- for(i = 1; i < keysSorted.length; i += 1) {
- if(max === words[keysSorted[i]]) {
- sequence += 1;
- }
- else {
- break;
- }
- }
- for(i = 0; i < sequence; i++) {
- console.log(keysSorted[i] + ' -> ' + words[keysSorted[i]] + ' times');
- }
- }
- findMostFreqWord('in the middle of the night');
- findMostFreqWord('Welcome to SoftUni. Welcome to Java. Welcome everyone.');
- findMostFreqWord('Hello my friend, hello my darling. Come on, come here. Welcome, welcome darling.');
Advertisement
Add Comment
Please, Sign In to add comment