Advertisement
satishfrontenddev4

Untitled

Jan 5th, 2024
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. You are given a list of words present in a book. Your younger brother is really curious to know the K most frequent words in the book, you have to find them.
  3.  
  4.  
  5. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order should come first.
  6.  
  7. Input format
  8. There are three lines of input
  9.  
  10. The first line contains N, which is the number of input strings.
  11.  
  12. The second line contains N space separated input strings (S).
  13.  
  14. The third line contains the value of K.
  15.  
  16. Output format
  17. Print the K most frequent words present inside the book, each in a new line. If two words have the same frequency, then the word with the lower alphabetical order should come first.
  18.  
  19. Constraints
  20. 1 <= N <= 1000
  21.  
  22. 1 <= Length(S) <= 100000
  23.  
  24. 'a' <= S[i] <= 'z'
  25.  
  26. 1 <= K <= Number of Unique Strings
  27.  
  28. Sample Input 1
  29. 1
  30.  
  31. bus
  32.  
  33. 1
  34.  
  35. Sample Output 1
  36. bus
  37.  
  38. Explanation 1
  39. In the given input, the frequency of "bus" is 1 and it is the 1st most frequent word.
  40.  
  41. Sample Input 2
  42. 3
  43.  
  44. car bus car
  45.  
  46. 2
  47.  
  48. Sample Output 2
  49. car
  50.  
  51. bus
  52.  
  53. Explanation 2
  54. In the given input, the frequency of "car" is 2, frequency of “bus” is 1.
  55.  
  56. Since K = 2, both words are printed, with "car" being the most frequent, gets printed first.
  57. */
  58.  
  59. /**
  60.  * @param {string[]} n
  61.  * @param {number} k
  62.  * @return {string[]}
  63.  */
  64. function frequentWords(words, k) {
  65.      const mp= new Map();
  66.      const len=words.length;
  67.      words.map(word=>{
  68.          if(mp.get(word)){
  69.              mp.set(word,mp.get(word)+1);
  70.          }else{
  71.              mp.set(word,1);
  72.          }
  73.      })
  74.     // map<word,itsFrequency>
  75.     let  arr= new Array();
  76.     // arr=[{fre,word},{},{},...]
  77.     // console.log(mp)
  78.     mp.forEach((value,key)=>{
  79.         arr.push({fre:value,word:key});
  80.     });
  81.  
  82.     // console.log(arr);
  83.     arr=arr.sort((a,b)=>{
  84.         const fre1=a.fre;
  85.         const fre2=b.fre;
  86.         const word1=a.word;
  87.         const word2=b.word;
  88.         if(fre1==fre2){
  89.             return word1.localeCompare(word2);
  90.         }
  91.         return fre2-fre1;
  92.     })
  93.  
  94.     const ans= arr.slice(0,k).map(ele=>ele.word);
  95.     return ans;
  96. }
  97.  
  98. function main() {
  99.     const n = parseInt(readLine())
  100.     const words = readLine().split(' ')
  101.     const k = parseInt(readLine())
  102.     const result = frequentWords(words, k)
  103.     console.log(result.join("\n"))
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement