import java.util.
HashMap;
import java.util.Map;
public class DuplicateWordsCounter {
public static void main(String[] args) {
String str = "java code to count the number of duplicate words in given
Strings";
Map<String, Integer> wordCount = new HashMap<>();
// Split the string into words
String[] words = str.split("\\s+");
// Count the occurrences of each word
for (String word : words) {
// Convert to lowercase to ignore case sensitivity
String lowerCaseWord = word.toLowerCase();
if (wordCount.containsKey(lowerCaseWord)) {
// If the word is already in the map, increment its count
wordCount.put(lowerCaseWord, wordCount.get(lowerCaseWord) + 1);
} else {
// If the word is not in the map, add it with a count of 1
wordCount.put(lowerCaseWord, 1);
}
}
// Print duplicate words and their counts
System.out.println("Duplicate words:");
for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
if (entry.getValue() > 1) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
}
////////////////////Java program to find the duplicate words in a
string//////////////////////////////////////////
Java program to find the duplicate words in a string
public class DuplicateWord {
public static void main(String[] args) {
String string = "Big black bug bit a big black dog on his big black nose";
int count;
//Converts the string into lowercase
string = string.toLowerCase();
//Split the string into words using built-in function
String words[] = string.split(" ");
System.out.println("Duplicate words in a given string : ");
for(int i = 0; i < words.length; i++) {
count = 1;
for(int j = i+1; j < words.length; j++) {
if(words[i].equals(words[j])) {
count++;
//Set words[j] to 0 to avoid printing visited word
words[j] = "0";
}
}
//Displays the duplicate word if count is greater than 1
if(count > 1 && words[i] != "0")
System.out.println(words[i]);
}
}
}
///////////////////////////////////////////////////
public class StringsCount{
public static void main(String args[]) {
String value = "This is testing Program testing Program";
String item[] = value.split(" ");
HashMap<String, Integer> map = new HashMap<>();
for (String t : item) {
if (map.containsKey(t)) {
map.put(t, map.get(t) + 1);
} else {
map.put(t, 1);
}
}
Set<String> keys = map.keySet();
for (String key : keys) {
System.out.println(key);
System.out.println(map.get(key));
}
}
}
//////////////////////////////////////////////////////////////////
String words = "I go to summer school. I also go to winter school";
int counter = 0;
if(words.contains("go to")) {
counter++;
}
System.out.println("counter " + counter);
////////////////////////////////////
Java program to find all duplicate characters in a string
public class Example {
public static void main(String argu[]) {
String str = "beautiful beach";
char[] carray = str.toCharArray();
System.out.println("The string is:" + str);
System.out.print("Duplicate Characters in above string are: ");
for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j < str.length(); j++) {
if (carray[i] == carray[j]) {
System.out.print(carray[j] + " ");
break;
}
}
}
}
}
Output
The string is:beautiful beach
Duplicate Characters in above string are: b e a u
///////////////////////////////////////////////////////////////////////////////