Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Code to Copy:
- //Import the required packages.
- import java.util.Scanner;
- //Define the class FindFrequency.
- public class FindFrequency {
- //Define the method() GetFrequencyOfWord().
- public static int GetFrequencyOfWord(String[] wordsList, int listSize, String currWord)
- {
- //Declare a variable to store
- //the frequency.
- int frequency = 0;
- //Run the loop to traverse the
- //list containing the words..
- for(int i = 0; i < listSize; ++i)
- {
- //If the current word in the list is equal
- //to the currWord then, increase
- //the count of the frequency.
- if(wordsList[i].equals(currWord)) {
- frequency++;
- }
- }
- //Return the frequency.
- return frequency;
- }
- //Define the main() method.
- public static void main(String[] args) {
- //Define an object of Scanner class
- //to get the input.
- Scanner input = new Scanner(System.in);
- //Declare a variable to store the
- //number of words in the input.
- int listSize;
- listSize = input.nextInt();
- //Define an array of Strings to store the words.
- String[] wordsList = new String[listSize];
- //Run the loop to read and store
- //the words from the input.
- for(int i = 0; i < listSize; i++) {
- wordsList[i] = input.next();
- }
- //Define an array of integers to store the frequencies.
- int[] FrequencyList = new int[listSize];
- //Run the loop to find the frequency of each word.
- for(int i = 0; i < listSize; i++)
- {
- //Call the method GetFrequencyOfWord() to
- //get the frequency of the current word.
- FrequencyList[i] =
- GetFrequencyOfWord(wordsList, wordsList.length, wordsList[i]);
- }
- //Run the loop to find the frequency of each word.
- for(int i = 0; i < listSize; i++)
- {
- //Call the method GetFrequencyOfWord() to
- //get the frequency of the current word.
- System.out.println(wordsList[i] + " " + FrequencyList[i]);
- }
- //Close the scanner object.
- input.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement