Finding repeated words on a string and counting the repetitions

c++ program to count number of repeated words in a string
find frequency of each word in a string in java
find duplicate words in string c#
count repeated characters in a string in java
count repeated words in a string in python
remove duplicate words from string in c
count of words in a string
count the occurrence of a word in a file in java

I need to find repeated words on a string, and then count how many times they were repeated. So basically, if the input string is this:

String s = "House, House, House, Dog, Dog, Dog, Dog";

I need to create a new string list without repetitions and save somewhere else the amount of repetitions for each word, like such:

New String: "House, Dog"

New Int Array: [3, 4]

Is there a way to do this easily with Java? I've managed to separate the string using s.split() but then how do I count repetitions and eliminate them on the new string? Thanks!

You've got the hard work done. Now you can just use a Map to count the occurrences:

Map<String, Integer> occurrences = new HashMap<String, Integer>();

for ( String word : splitWords ) {
   Integer oldCount = occurrences.get(word);
   if ( oldCount == null ) {
      oldCount = 0;
   occurrences.put(word, oldCount + 1);

Using map.get(word) will tell you many times a word occurred. You can construct a new list by iterating through map.keySet():

for ( String word : occurrences.keySet() ) {
  //do something with word

Note that the order of what you get out of keySet is arbitrary. If you need the words to be sorted by when they first appear in your input String, you should use a LinkedHashMap instead.

Finding repeated words on a string and counting the repetitions , You are given a string and a word your task is that count the number of the Now run a loop at 0 to length of string and check if our string is equal to the word of a character in a repeated string · Python | Count occurrences of a character in to the frequency of characters · Remove duplicates from string keeping the order  It also count number of repeated words. It also can remove all the repetitions from the text. The calculator below gives the number of words in a text, number of unique words (total number of words minus all word repetirions). The calculator can also remove all the repetitions from the given text.

As mentioned by others use String::split(), followed by some map (hashmap or linkedhashmap) and then merge your result. For completeness sake putting the code.

import java.util.*;

public class Genric<E>
    public static void main(String[] args) 
        Map<String, Integer> unique = new LinkedHashMap<String, Integer>();
        for (String string : "House, House, House, Dog, Dog, Dog, Dog".split(", ")) {
            if(unique.get(string) == null)
                unique.put(string, 1);
                unique.put(string, unique.get(string) + 1);
        String uniqueString = join(unique.keySet(), ", ");
        List<Integer> value = new ArrayList<Integer>(unique.values());

        System.out.println("Output = " + uniqueString);
        System.out.println("Values = " + value);


    public static String join(Collection<String> s, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        Iterator<String> iter = s.iterator();
        while (iter.hasNext()) {
            if (iter.hasNext()) {
        return buffer.toString();

New String is Output = House, Dog

Int array (or rather list) Values = [3, 4] (you can use List::toArray) for getting an array.

Count occurrences of a word in string, Java program to find repeated words or duplicate words in string or text file. Java example to count occurances of each word to find unique words in string. the order just incase if you have to recreate the String removing the duplicates). It can help you in to find the most frequent words in a string also check the count which will be equal to one for unique words. Step1: Split the words from the input String using the split() method. Step2: Iterate the loop for counting the repeated words.

Try this,

public class DuplicateWordSearcher {
public static void main(String[] args) {

    String text = "a r b k c d se f g a d f s s f d s ft gh f ws w f v x s g h d h j j k f sd j e wed a d f";

    List<String> list = Arrays.asList(text.split(" "));

    Set<String> uniqueWords = new HashSet<String>(list);
    for (String word : uniqueWords) {
        System.out.println(word + ": " + Collections.frequency(list, word));


How to Find Duplicate Words in String in Java, It can help you in to find the most frequent words in a string also check the count which will be equal to one for unique words. Step1: Split the  If count is greater than 1, it implies that a word has duplicate in the string. In above example, the words highlighted in green are duplicate words. Algorithm. Define a string. Convert the string into lowercase to make the comparison insensitive. Split the string into words. Two loops will be used to find duplicate words. Outer loop will select a word and Initialize variable count to 1.

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) {


Java Program to Count repeated words in String, Program. //Program to count repeated words in string public class CountWords { public static void main(String[] args) { String input="Welcome to  Java Program to Find Repeated Words in a String. The following Java program prints repeated/duplicated words in a String. The program uses case insensitive comparison (For example, program assumes words CAT, cat and Cat etc. are all same). This algorithm is useful in text processing programs where word frequency calculations are needed.

Using java8

private static void findWords(String s, List<String> output, List<Integer> count){
    String[] words = s.split(", ");
    Map<String, Integer> map = new LinkedHashMap<>();>map.put(e, map.getOrDefault(e, 0) + 1));

Also, use a LinkedHashMap if you want to preserve the order of insertion

private static void findWords(){
    String s = "House, House, House, Dog, Dog, Dog, Dog";
    List<String> output = new ArrayList<>();
    List<Integer> count = new ArrayList<>();
    findWords(s, output, count);


[House, Dog]
[3, 4]

Java program to count number of duplicate words in given string, Problem : Write a Java program to print the duplicate words from a given statement instead of HashSet to keep count of duplicate words in the sentence. + duplicates); } /** * Method to find duplicate words in a Sentence or  Free software utility which allows you to find the most frequent phrases and frequencies of words. Non-English language texts are supported. It also counts number of words, characters, sentences and syllables. Also calculates lexical density.

How to find duplicate words in Java String? [Solution], are all same). This algorithm is useful in text processing programs where word frequency calculations are needed. The program first asks for the input string from  count = {} for s in check_string: if s in count: count[s] += 1 else: count[s] = 1 for key in count: if count[key] > 1: print key, count[key] This ensures that you only go through the string once, instead of 26 times.

Java Program to Find Repeated Words in a String, Java program to print count of each word in a string and find repeating words : In this tutorial, we will learn how to print the count of each word in  Word Frequency Counter. Our word frequency counter allows you to count the frequency usage of each word in your text. Paste or type in your text below, and click submit. Also try our Phrase Frequency Counter.

Java program to find count of words and find repeating words in a , Given a string, the task is to find maximum consecutive repeating character in string. Note : We do not need to consider overall count, but the count of repeating that appear at one place. The Simple solution of this problem is to use two for loops. The outer loop considers current character, the inner loop counts occurrences of current character.

  • using map.get(word) returns null. here's the documentation: ----LATER EDIT: i saw that your key is String, so yea, it works, my bad.
  • Hi @Favonius.I want to load the strings from a text file and apply your code,Like this I want to get 2000+text files each separately apply your code and get output...Is this Possible?
  • this does not need a nested loop. a linear parse over the words array would be good enough to check if a HashMap key exists, increment, else add an entry and set value to zero
  • Few words would be better to explain your code, instead of just code.
  • 1. In Hashmap , words will be the key and occurance will be value . If we are putting word first time in a hash map and related value will be 1 . Otherwise , we are incremented the value by 1 everytime when we are putting the same word .
  • Please put this into the answer section not on the comment section.
  • Place some more explanation to your answer, not only code.
  • Welcome to StackOverflow! Please take some time to read the help page about how to ask questions. How is your answer different from all of those already provided?
  • An explanation might be helpful as well.