Java Program to Count the Number of Unique Words

«
»

This is the Java Program to Find the Number of Unique Words.

Problem Description

Given a string, in which various words are separated by whitespace. Print the count of the unique words present in it, that is the words whose frequency is 1.

Example:
String str =”Java is great C++ is also great”;

Output = 3

Problem Solution

Split the string into various substrings based on the delimiter whitespace. Now find the frequency of each substring and maintain a count of substrings whose frequency is 1.

Program/Source Code

Here is the source code of the Java Program to Find the Number of Unique Words. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
  1.  
  2. //Java Program to Find the Number of Unique Words
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7.  
  8. public class NumberOfUniqueWords {
  9.     // Function to calculate the number of unique words
  10.     static int calculateNoOfUniqueWords(String str) {
  11.         String[] words = str.split(" ");
  12.         boolean[] array = new boolean[words.length];
  13.         int j, i = 0;
  14.         int count = 0;
  15.         for (i = 0; i < words.length; i++) {
  16.             if (!array[i]) {
  17.                 count++;
  18.                 for (j = i + 1; j < words.length; j++) {
  19.                     if (words[j].compareTo(words[i]) == 0) {
  20.                         array[j] = true;
  21.                         count--;
  22.                     }
  23.                 }
  24.             }
  25.         }
  26.         return count;
  27.     }
  28.     // Function to read input and display the output
  29.     public static void main(String[] args) {
  30.         BufferedReader br = new BufferedReader
  31.                             (new InputStreamReader(System.in));
  32.         String str;
  33.         System.out.println("Enter the string");
  34.         try {
  35.             str = br.readLine();
  36.         } catch (IOException e) {
  37.             System.out.println("An I/O error occurred");
  38.             return;
  39.         }
  40.         int count = calculateNoOfUniqueWords(str);
  41.         System.out.println("Total number of unique words in \"" + 
  42.                                          str + "\" are " + count);
  43.     }
  44. }
Program Explanation

1. In function calculateNoOfUniqueWords, the split method is used to split the string into various substrings, which are separated by whitespaces.
2. An array of boolean values is maintained to avoid checking the repeated substring more than once.
3. The loop for (i = 0; i < words.length; i++) is used to iterate through the array of substrings.
4. The condition if(!array[i]), checks whether the substring is already checked or not.
5. The nested loop checks if the substring array[i] is repeated or not.
6. If the substring is not repeated, the variable count is incremented.
7. Finally, the variable count is returned.

Time Complexity: O(n2) where n is the length of the string.

Runtime Test Cases
 
Case 1 (Average Case):
 
Enter the string
Java is great C++ is also great
Total number of unique words in "Java is great C++ is also great" are 3
 
Case 2 (Best Case):
 
Enter the string
I am am I
Total number of unique words in "I am am I" are 0
 
Case 3 (Worst Case):
 
Enter the string
We are here
Total number of unique words in "We are here" are 3

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.