• STEP I: Getting the user input: We just need one input from the user that will be used to count the number of words in it.

  • STEP II: Splitting the user input into constituent words so that we can count them easily.

  • STEP III: Now we need to create a hash where we can store the value of each word. Remember to initialize the Hash with some value to avoid any form of errors.

  • STEP IV: Now we need to iterate over our array and add the words to the Hash so that we can store the frequencies with them.

  • STEP V: Now that we have stored the frequency of each word present in the sentence, we need to sort it in the descending order.

  • STEP VI: Finally we just need to print the key value pairs.


Try in the REPL