
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding n most frequent words from a sentence in JavaScript
For the purpose of this question, we define a sentence as a string that contains English alphabets and punctuations and a word is a substring of that sentence joined together by whitespaces.
We are required to write a JavaScript function that takes in a sentence string, str, as the first argument and a number, num, as the second argument. The function should first count the frequency of each word in the sentence and then return an array of length num containing num most frequent words placed according to decreasing frequencies.
For example −
If the input sentence and the number is −
const str = 'i am a good coder and i know that i can solve a problem'; const num = 2;
Then the output should be −
const output = ['i', 'a'];
because 'i' appears for 3 times while 'a' appears for 2 times in the array and they are the 2 most frequent words in the string.
Example
The code for this will be −
const str = 'i am a good coder and i know that i can solve a problem'; const num = 2; const findMostFrequent = (str = '', num = 1) => { const strArr = str.split(' '); const map = {}; strArr.forEach(word => { if(map.hasOwnProperty(word)){ map[word]++; }else{ map[word] = 1; } }); const frequencyArr = Object.keys(map).map(key => [key, map[key]]); frequencyArr.sort((a, b) => b[1] - a[1]); return frequencyArr.slice(0, num).map(el => el[0]); }; console.log(findMostFrequent(str, num));
Output
And the output in the console will be −
[ 'i', 'a' ]
- Related Articles
- Finding the second most frequent character in JavaScript
- Finding the most frequent word(s) in an array using JavaScript
- Find the k most frequent words from data set in Python
- Finding top three most occurring words in a string of text in JavaScript
- Second most frequent character in a string - JavaScript
- Counting number of words in a sentence in JavaScript
- Returning the second most frequent character from a string (including spaces) - JavaScript
- Python Program to crawl a web page and get most frequent words
- Finding words in a matrix in JavaScript
- Find Second most frequent character in array - JavaScript
- Finding duplicate "words" in a string - JavaScript
- Arranging words by their length in a sentence in JavaScript
- Reverse all the words of sentence JavaScript
- Find the second most frequent element in array JavaScript
- Finding the length of second last word in a sentence in JavaScript
