Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. Code to Copy:
  2.  
  3. //Import the required packages.
  4.  
  5. import java.util.Scanner;
  6.  
  7. //Define the class FindFrequency.
  8.  
  9. public class FindFrequency {
  10.  
  11. //Define the method() GetFrequencyOfWord().
  12.  
  13. public static int GetFrequencyOfWord(String[] wordsList, int listSize, String currWord)
  14.  
  15. {
  16.  
  17. //Declare a variable to store
  18.  
  19. //the frequency.
  20.  
  21. int frequency = 0;
  22.  
  23. //Run the loop to traverse the
  24.  
  25. //list containing the words..
  26.  
  27. for(int i = 0; i < listSize; ++i)
  28.  
  29. {
  30.  
  31. //If the current word in the list is equal
  32.  
  33. //to the currWord then, increase
  34.  
  35. //the count of the frequency.
  36.  
  37. if(wordsList[i].equals(currWord)) {
  38.  
  39. frequency++;
  40.  
  41. }
  42.  
  43. }
  44.  
  45. //Return the frequency.
  46.  
  47. return frequency;
  48.  
  49. }
  50.  
  51. //Define the main() method.
  52.  
  53. public static void main(String[] args) {
  54.  
  55. //Define an object of Scanner class
  56.  
  57. //to get the input.
  58.  
  59. Scanner input = new Scanner(System.in);
  60.  
  61. //Declare a variable to store the
  62.  
  63. //number of words in the input.
  64.  
  65. int listSize;
  66.  
  67. listSize = input.nextInt();
  68.  
  69. //Define an array of Strings to store the words.
  70.  
  71. String[] wordsList = new String[listSize];
  72.  
  73. //Run the loop to read and store
  74.  
  75. //the words from the input.
  76.  
  77. for(int i = 0; i < listSize; i++) {
  78.  
  79. wordsList[i] = input.next();
  80.  
  81. }
  82.  
  83. //Define an array of integers to store the frequencies.
  84.  
  85. int[] FrequencyList = new int[listSize];
  86.  
  87. //Run the loop to find the frequency of each word.
  88.  
  89. for(int i = 0; i < listSize; i++)
  90.  
  91. {
  92.  
  93. //Call the method GetFrequencyOfWord() to
  94.  
  95. //get the frequency of the current word.
  96.  
  97. FrequencyList[i] =
  98.  
  99. GetFrequencyOfWord(wordsList, wordsList.length, wordsList[i]);
  100.  
  101. }
  102.  
  103. //Run the loop to find the frequency of each word.
  104.  
  105. for(int i = 0; i < listSize; i++)
  106.  
  107. {
  108.  
  109. //Call the method GetFrequencyOfWord() to
  110.  
  111. //get the frequency of the current word.
  112.  
  113. System.out.println(wordsList[i] + " " + FrequencyList[i]);
  114.  
  115. }
  116.  
  117.  
  118.  
  119. //Close the scanner object.
  120.  
  121. input.close();
  122.  
  123. }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement