Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- const wordsOccurence = {};
- const wordsToCheck = input.shift().split(" "); // ['this', 'sentence']
- let counter = 0;
- for (const word of wordsToCheck) {
- for (const str of input) {
- if (word == str) {
- if (!wordsOccurence.hasOwnProperty(word)) {
- counter++;
- } else {
- counter = wordsOccurence[word];
- counter++;
- }
- wordsOccurence[word] = counter;
- counter = 0;
- }
- }
- }
- let sorted = Object.entries(wordsOccurence).sort((a, b) => b[1] - a[1]);
- for (const [word, count] of sorted) {
- console.log(`${word} - ${count}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment