Table Of Contents
In this Program we are going to Search Duplicate Words in the Given String and Thus Display those words.
Lets Take Example: Welcome to Noteshacker Noteshacker name is good. here Duplicate word is “Noteshacker” and count is 3
For Finding Duplicate Words in a given String,First we have to Split String into Words and then check for duplicate words.
If count >1 then we got duplicate word print the given word along with its counter value.
Algorithm/Program Explanation
- Start
- Take String Input String s1.
- Split the String into words
- Define int count =1.
- Define and initialize Two For Loop
- for(int i=0;i<split.length;i++)
- for(int j=i+1;j<split.length;j++)
- Check for Conditions
- if(split[i].equals(split[j]))
- count=count+1;
- split[j]=” # “;//Replacing Repeated by #
- Check for Conditions
- if(split[i] !=”#”)
- print( split[i]+ ” Count =” + count)
- End
Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /* Java Program To Count Duplicate Words */ import java.util.*; public class count{ public static void main(String args[]){ String s1= "Welcome to Noteshacker Noteshacker Noteshacker" ; //String Input String split[]=s1.split( " " ); // Splitting String into Words int count= 1 ; for ( int i= 0 ;i<split.length;i++){ for ( int j=i+ 1 ;j<split.length;j++){ if (split[i].equals(split[j])){ count=count+ 1 ; split[j]= " # " ; //Replacing Repeated by # } } if (split[i] != "#" && count> 1 ) //printing only Duplicate words { System.out.println( split[i]+ " Count =" + count); count= 1 ; } } } } |
Output:
Duplicate Words in String=Noteshacker =3
Duplicate Words in String= # =2// this of no use
Related Topics
- Arithmetic Operations Java Program
- Java program to perform Logical and Relational operations
- Java program to Implement Mathematical functions defined in Java Math class
- Java basic Programs with Explanation
- Java Program to find Min,Max Element in an Array
- Java Program to sort Array Elements in ascending and descending order
- Java Program to display Array item at Even & Odd position
- Program to Calculate Sum and Average of Array elements
- Java Program to find Index of specific Element in an array
- Program to find common elements between two arrays
- Java Program to Add/Insert specific Element to an Array
- Java Program to delete specific element from an Array
- Program to calculate frequency of each element in Array
- Java Program to find Even and Odd Numbers in the arrays
- Java Program to search Given Number in an Array
- Java Program to Add or Sub Two Matrices
- Java Program to Multiply two or More Matrices
- Java Program to Print Array in Reverse Order
- Java Program To Transpose Given Matrix
- Java Programs List- Java Programs Examples With Output