In this post, we will write a simple Java program to count a number of duplicate words in a given string.
In this program, please refer the comments, each line of code is explained via comments.
Java Program to Count Number of Duplicate Words in Given String
import java.util.HashMap; import java.util.Map; import java.util.Set; /** * Java program to count number of duplicate words in given string * @author javaguides.net * */ public class DuplicateWordsInString { private static void duplicateWords(String inputString) { // Splitting inputString into words final String[] words = inputString.split(" "); // Creating one HashMap with words as key and their count as value final Map < String, Integer > wordCount = new HashMap < String, Integer > (); // Checking each word for (String word: words) { // whether it is present in wordCount if (wordCount.containsKey(word)) { // If it is present, incrementing it's count by 1 wordCount.put(word, wordCount.get(word) + 1); } else { // If it is not present, put that word into wordCount with 1 as // it's value wordCount.put(word, 1); } } // Extracting all keys of wordCount final Set < String > wordsInString = wordCount.keySet(); // Iterating through all words in wordCount for (String word: wordsInString) { // if word count is greater than 1 if (wordCount.get(word) > 1) { // Printing that word and it's count System.out.println(word + " : " + wordCount.get(word)); } } } public static void main(String[] args) { duplicateWords("java guides java"); duplicateWords("Java is java again java"); duplicateWords("Super Man Bat Man Spider Man"); } }
Output:
java : 2
java : 2
man : 2
Let's analysis and understand the above program:
1. Used split() method to split input String into words.
2. We used HashMap to store key, value pair that is a word with its count.
final Map<String, Integer> wordCount = new HashMap<String, Integer>();3. Used containsKey method of HashMap to check whether the word present or not. If HashMap contains word then increment its value by 1 and If a word is not present, put that word as key and value as 1.
4. Finally, iterate over HashMap keyset and check with each key's value, if it is greater than 1 then it is a duplicate word.
Related String Programs
Note that these programs are asked in interviews.- Java program to Count Number of Duplicate Words in String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains only Digits
- How to Check if the String Contains only Letters
- How to Check If the String Contains Only Letters or Digits
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course