In the previous article, we have discussed about Java Program to Reverse Sequence of Words of a Sentence
In this article we will see how to count and show the repeated words in a string without using Hashmap in Java programming language.
Java Program to Count and Show the Repeated Words in a String without using HashMap
As per the problem statement, it requires to count and show repeated words in the String without Hashmap. So, let’s do it by using array.
For example:
If the string is "abc bca cba abc abc cba" Then the duplicate strings are- abc=3 cba=2
Let’s see the program to understand it more clearly.
- Java Program to Count and Show the Repeated Words in a String without using HashMap (By Using Array) & Static Input Value
- Java Program to Count and Show the Repeated Words in a String without using HashMap (By Using Array) & User Input Value
Method-1: Java Program to Count and Show the Repeated Words in a String without using HashMap (By Using Array) & Static Input Value
Approach:
- Declare a string variable and initialize it’s value.
- Convert that string value to lowercase by using
toLowerCase()
method, which will be easy to compare. - Then split the string based on space by using
split()
method and store the string elements in an String array. - Then compare each elements with other elements of string array by using
equals()
method and keep track on occurrence of values. - Then by using an if condition check the elements whose occurrence is greater than 1, those elements are repeated words.
Program:
public class Main { public static void main(String[] args) { String str = "abc bca cba abc abc cba"; int count; //Converting the string into lowercase which will be easy to compare str = str.toLowerCase(); //splitted string based on space String word[] = str.split(" "); System.out.println("Duplicate words in a given string : "); for(int i = 0; i < word.length; i++) { // initializing count as 1 count = 1; //comparing each word with other words till last for(int j = i+1; j < word.length; j++) { if(word[i].equals(word[j])) { count++; //it will not print visited word word[j] = "0"; } } //duplicate word if count is greater than 1 if(count > 1 && word[i] != "0") System.out.println(word[i]+"="+count); } } }
Output:
Duplicate words in a given string : abc=3 cba=2
Method-2: Java Program to Count and Show the Repeated Words in a String without using HashMap (By Using Array) & User Input Value
import java.util.*; public class Main { public static void main(String[] args) { int count; Scanner sc = new Scanner(System. in ); System.out.println("Enter a string/sentence"); String str = sc.nextLine(); //Converting the string into lowercase which will be easy to compare str = str.toLowerCase(); //splitted string based on space String word[] = str.split(" "); System.out.println("Duplicate words in a given string : "); for(int i = 0; i < word.length; i++) { // initializing count as 1 count = 1; //comparing each word with other words till last for(int j = i+1; j < word.length; j++) { if(word[i].equals(word[j])) { count++; //it will not print visited word word[j] = "0"; } } //duplicate word if count is greater than 1 if(count > 1 && word[i] != "0") System.out.println(word[i]+"="+count); } } }
Output:
Enter a string/sentence I love Java I love BtechGeeks BtechGeeks BtechGeeks Duplicate words in a given string : i=2 love=2 btechgeeks=3
Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.
Related Java Programs: