Find the Duplicate Words in a String & File: This Java program aims to count the frequency of repeated words in a given input string and display the results. It does so by splitting the input string into individual words using the space character as a delimiter and then using nested loops to compare each word with the rest of the words to identify repeated occurrences.
Find the Duplicate Words in a String
package com.softwaretestingo.interviewprograms; public class InterviewPrograms90 { //Java Program to Count repeated words in String public static void main(String[] args) { String input="Welcome to Java Session Session Session"; String[] words=input.split(" "); int wrc=1; for(int i=0;i<words.length;i++) { for(int j=i+1;j<words.length;j++) { if(words[i].equals(words[j])) { wrc=wrc+1; words[j]="0"; } } if(words[i]!="0") System.out.println(words[i]+"--"+wrc); wrc=1; } } }
Output
Welcome--1 to--1 Java--1 Session--3
Alternative Way 1:
This Java program aims to count and identify duplicated or repeated words in a given input string and display the results. It uses a Scanner class to read user input from the console and then utilizes a HashMap to keep track of unique words and duplicated words. The program performs a case-insensitive comparison to handle words in different letter cases.
package com.softwaretestingo.interviewprograms; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class InterviewPrograms90_1 { //Java Program to Count repeated words in String public static void main(String[] args) { System.out.print("Enter string to analyse:"); Scanner sn = new Scanner(System.in); String input = sn.nextLine(); // first let us split string into words String[] words = input.split(" "); // adds all words into a map // we also check whether the word is already in map! Map<String,String> wordMap = new HashMap<String,String>(); Map<String,String> printedMap = new HashMap<String,String>(); for(int i=0;i<words.length;i++) { String word = words[i].toUpperCase(); // for case insensitive comparison if(wordMap.get(word)!=null) { // we found a duplicated word! if(printedMap.get(word)==null) { // first check if it is printed already! System.out.println("Duplicated/Repeated word:"+word); printedMap.put(word, word); } }else { wordMap.put(word, word); } } } }
Output
Enter string to analyse:Welcome to software testing blog to blog Duplicated/Repeated word:TO Duplicated/Repeated word:BLOG
Alternative Way 2:
This Java program aims to read a text file, count the occurrences of each word, and then display the repeated words along with their occurrences in descending order of frequency. It utilizes various Java classes like BufferedReader, FileReader, HashMap, ArrayList, and Entry to achieve this.
Find the Duplicate Words in a File
package com.softwaretestingo.interviewprograms; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import java.util.Set; public class InterviewPrograms91 { //Java Program to Count repeated words in Text File public static void main(String[] args) { HashMap<String, Integer> wordCountMap = new HashMap<String, Integer>(); BufferedReader reader = null; try { //Creating BufferedReader object reader = new BufferedReader(new FileReader("C:\\Users\\XXXXX\\git\\javapgms\\Java_Programs\\testdata.txt")); //Reading the first line into currentLine String currentLine = reader.readLine(); while (currentLine != null) { //splitting the currentLine into words String[] words = currentLine.toLowerCase().split(" "); //Iterating each word for (String word : words) { //if word is already present in wordCountMap, updating its count if(wordCountMap.containsKey(word)) { wordCountMap.put(word, wordCountMap.get(word)+1); } //otherwise inserting the word as key and 1 as its value else { wordCountMap.put(word, 1); } } //Reading next line into currentLine currentLine = reader.readLine(); } //Getting all the entries of wordCountMap in the form of Set Set<Entry<String, Integer>> entrySet = wordCountMap.entrySet(); //Creating a List by passing the entrySet List<Entry<String, Integer>> list = new ArrayList<Entry<String,Integer>>(entrySet); //Sorting the list in the decreasing order of values Collections.sort(list, new Comparator<Entry<String, Integer>>() { @Override public int compare(Entry<String, Integer> e1, Entry<String, Integer> e2) { return (e2.getValue().compareTo(e1.getValue())); } }); //Printing the repeated words in input file along with their occurrences System.out.println("Repeated Words In Input File Are :"); for (Entry<String, Integer> entry : list) { if (entry.getValue() > 1) { System.out.println(entry.getKey() + " : "+ entry.getValue()); } } } catch (IOException e) { e.printStackTrace(); } finally { try { reader.close(); //Closing the reader } catch (IOException e) { e.printStackTrace(); } } } }
Output
Repeated Words In Input File Are : java : 5 jdbc : 3 jsf : 3 hibernate : 2 spring : 2