11

I am writing a very basic java program that calculates frequency of each word in a sentence so far i managed to do this much

import java.io.*;

class Linked {

    public static void main(String args[]) throws IOException {

        BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in));
        System.out.println("Enter the sentence");
        String st = br.readLine();
        st = st + " ";
        int a = lengthx(st);
        String arr[] = new String[a];
        int p = 0;
        int c = 0;

        for (int j = 0; j < st.length(); j++) {
            if (st.charAt(j) == ' ') {
                arr[p++] = st.substring(c,j);
                c = j + 1;
            }
        }
    }

    static int lengthx(String a) {
        int p = 0;
        for (int j = 0; j < a.length(); j++) {
            if (a.charAt(j) == ' ') {
                p++;
            }
        }
        return p;
    }
}

I have extracted each string and stored it in a array , now problem is actually how to count the no of instances where each 'word' is repeated and how to display so that repeated words not get displayed multiple times , can you help me in this one ?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Sigma
  • 742
  • 2
  • 9
  • 24

20 Answers20

23

Use a map with word as a key and count as value, somthing like this

    Map<String, Integer> map = new HashMap<>();
    for (String w : words) {
        Integer n = map.get(w);
        n = (n == null) ? 1 : ++n;
        map.put(w, n);
    }

if you are not allowed to use java.util then you can sort arr using some sorting algoritm and do this

    String[] words = new String[arr.length];
    int[] counts = new int[arr.length];
    words[0] = words[0];
    counts[0] = 1;
    for (int i = 1, j = 0; i < arr.length; i++) {
        if (words[j].equals(arr[i])) {
            counts[j]++;
        } else {
            j++;
            words[j] = arr[i];
            counts[j] = 1;
        }
    }

An interesting solution with ConcurrentHashMap since Java 8

    ConcurrentMap<String, Integer> m = new ConcurrentHashMap<>();
    m.compute("x", (k, v) -> v == null ? 1 : v + 1);
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Is there any other way to accomplish this , Because For my examination i am not allowed to use java.utill – Sigma Feb 14 '14 at 05:46
  • OK, see my no map version – Evgeniy Dorofeev Feb 14 '14 at 06:06
  • @EvgeniyDorofeev , thanks the logic looks really good but where is "arr" defined ? – grepit Jun 21 '14 at 22:45
  • @EvgeniyDorofeev This word really well for me, but I'm confused about the syntax here : n = (n == null) ? 1 : ++n; I'm new to Java, can you explain how it works or tell me where I can find out? – user25976 Apr 08 '16 at 05:40
  • @user25976 That is called a ternary operator. It is like an if-then-else statement. It is expressed as follows: (boolean condition)?(then):(else) In this case, we are checking to see if n equals null, if this condition is true, then we set the value of n to 1. If it is false, then we pre-increment the value of n by one. – Ebony Maw Apr 05 '17 at 03:54
  • @EvgeniyDorofeev Is it possible to utilize java.util.Collections.frequency() method to achieve what Sigma is asking? – Ebony Maw Apr 05 '17 at 03:56
15

In Java 8, you can write this in two simple lines! In addition you can take advantage of parallel computing.

Here's the most beautiful way to do this:

Stream<String> stream = Stream.of(text.toLowerCase().split("\\W+")).parallel();

Map<String, Long> wordFreq = stream
     .collect(Collectors.groupingBy(String::toString,Collectors.counting()));
Bahul Jain
  • 193
  • 1
  • 5
4
import java.util.*;

public class WordCounter {

    public static void main(String[] args) {

        String s = "this is a this is this a this yes this is a this what it may be i do not care about this";
        String a[] = s.split(" ");
        Map<String, Integer> words = new HashMap<>();
        for (String str : a) {
            if (words.containsKey(str)) {
                words.put(str, 1 + words.get(str));
            } else {
                words.put(str, 1);
            }
        }
        System.out.println(words);
    }
}

Output: {a=3, be=1, may=1, yes=1, this=7, about=1, i=1, is=3, it=1, do=1, not=1, what=1, care=1}

AKT
  • 182
  • 1
  • 8
3

Try this

public class Main
{

    public static void main(String[] args)
    {       
        String text = "the quick brown fox jumps fox fox over the lazy dog brown";
        String[] keys = text.split(" ");
        String[] uniqueKeys;
        int count = 0;
        System.out.println(text);
        uniqueKeys = getUniqueKeys(keys);

        for(String key: uniqueKeys)
        {
            if(null == key)
            {
                break;
            }           
            for(String s : keys)
            {
                if(key.equals(s))
                {
                    count++;
                }               
            }
            System.out.println("Count of ["+key+"] is : "+count);
            count=0;
        }
    }

    private static String[] getUniqueKeys(String[] keys)
    {
        String[] uniqueKeys = new String[keys.length];

        uniqueKeys[0] = keys[0];
        int uniqueKeyIndex = 1;
        boolean keyAlreadyExists = false;

        for(int i=1; i<keys.length ; i++)
        {
            for(int j=0; j<=uniqueKeyIndex; j++)
            {
                if(keys[i].equals(uniqueKeys[j]))
                {
                    keyAlreadyExists = true;
                }
            }           

            if(!keyAlreadyExists)
            {
                uniqueKeys[uniqueKeyIndex] = keys[i];
                uniqueKeyIndex++;               
            }
            keyAlreadyExists = false;
        }       
        return uniqueKeys;
    }
}

Output:

the quick brown fox jumps fox fox over the lazy dog brown
Count of [the] is : 2
Count of [quick] is : 1
Count of [brown] is : 2
Count of [fox] is : 3
Count of [jumps] is : 1
Count of [over] is : 1
Count of [lazy] is : 1
Count of [dog] is : 1
Zeeshan
  • 11,851
  • 21
  • 73
  • 98
2

From Java 10 you can use the following:

import java.util.Arrays;
import java.util.stream.Collectors;

public class StringFrequencyMap {
    public static void main(String... args){
        String[] wordArray = {"One", "One", "Two","Three", "Two", "two"};
        var freq = Arrays.stream(wordArray)
                         .collect(Collectors.groupingBy(x -> x, Collectors.counting()));
        System.out.println(freq);
    }
}

Output:

{One=2, two=1, Two=2, Three=1}
user2173372
  • 483
  • 7
  • 17
1

You could try this

public static void frequency(String s) {
    String trimmed = s.trim().replaceAll(" +", " ");
    String[] a = trimmed.split(" ");
    ArrayList<Integer> p = new ArrayList<>();
    for (int i = 0; i < a.length; i++) {
        if (p.contains(i)) {
            continue;
        }
        int d = 1;
        for (int j = i+1; j < a.length; j++) {
            if (a[i].equals(a[j])) {
                d += 1;
                p.add(j);
            }
        }
        System.out.println("Count of "+a[i]+" is:"+d);
    }
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Nhan
  • 119
  • 1
  • 6
1
package naresh.java;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class StringWordDuplicates {

    static void duplicate(String inputString){

        HashMap<String, Integer> wordCount = new HashMap<String,Integer>();
        String[] words = inputString.split(" ");

        for(String word : words){
            if(wordCount.containsKey(word)){
                wordCount.put(word, wordCount.get(word)+1);             
            }
            else{
                wordCount.put(word, 1);
            }
        }
        //Extracting of all keys of word count
        Set<String> wordsInString = wordCount.keySet();

        for(String word : wordsInString){
            if(wordCount.get(word)>1){
                System.out.println(word+":"+wordCount.get(word));
            }
        }

    }
    public static void main(String args[]){
        duplicate("I am Java Programmer and IT Server Programmer with Java as Best Java lover");

    }
}
0
class find
{
    public static void main(String nm,String w)
    {
        int l,i;
        int c=0;


        l=nm.length();String b="";

        for(i=0;i<l;i++)
        {
            char d=nm.charAt(i);
            if(d!=' ')
            {
                b=b+d;
            }
            if(d==' ')
            {
                if(b.compareTo(w)==0)
                {
                    c++;

                } 
               b="";           
            }        
        }       
        System.out.println(c);
    }
}
Community
  • 1
  • 1
0
public class wordFrequency {
    private static Scanner scn;

    public static void countwords(String sent) {
        sent = sent.toLowerCase().replaceAll("[^a-z ]", "");
        ArrayList<String> arr = new ArrayList<String>();
        String[] sentarr = sent.split(" ");
        Map<String, Integer> a = new HashMap<String, Integer>();
        for (String word : sentarr) {
            arr.add(word);
        }
        for (String word : arr) {
            int count = Collections.frequency(arr, word);
            a.put(word, count);
        }
        for (String key : a.keySet()) {
            System.out.println(key + " = " + a.get(key));
        }
    }

    public static void main(String[] args) {
        scn = new Scanner(System.in);
        System.out.println("Enter sentence:");
        String inp = scn.nextLine();
        countwords(inp);
    }

}
Rajesh NJ
  • 1
  • 1
0

Determine the frequency of words in a file.

File f = new File(fileName);
Scanner s = new Scanner(f);
Map<String, Integer> counts =
 new Map<String, Integer>(); 
while( s.hasNext() ){
 String word = s.next();
if( !counts.containsKey( word ) )
 counts.put( word, 1 );
else
 counts.put( word, 
  counts.get(word) + 1 );

}

jsingh
  • 1,256
  • 13
  • 24
0

The following program finds the frequency, sorts it accordingly, and prints it.

Below is the output grouped by frequency:

0-10:
       The   2
       Is    4
11-20:
       Have 13
       Done 15

Here is my program:

package com.company;
import java.io.*;
import java.util.*;
import java.lang.*;

/**
 * Created by ayush on 12/3/17.
 */

public class Linked {

    public static void main(String args[]) throws IOException {

        BufferedReader br = new BufferedReader(
                new InputStreamReader(System.in));
        System.out.println("Enter the sentence");
        String st = br.readLine();
        st=st.trim();
        st = st + " ";
        int count = lengthx(st);
        System.out.println(count);
        String arr[] = new String[count];
        int p = 0;
        int c = 0;

        for (int i = 0; i < st.length(); i++) {
            if (st.charAt(i) == ' ') {
                arr[p] = st.substring(c,i);
                System.out.println(arr[p]);
                c = i + 1;
                p++;
            }
        }
        Map<String, Integer> map = new HashMap<>();

        for (String w : arr) {
            Integer n = map.get(w);
            n = (n == null) ? 1 : ++n;
            map.put(w, n);
        }
        for (String key : map.keySet()) {
            System.out.println(key + " = " + map.get(key));
        }

        Set<Map.Entry<String, Integer>> entries = map.entrySet();

        Comparator<Map.Entry<String, Integer>> valueComparator = new Comparator<Map.Entry<String,Integer>>() {

            @Override
            public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2) {
                Integer v1 = e1.getValue();
                Integer v2 = e2.getValue();
                return v1.compareTo(v2); }
        };

        List<Map.Entry<String, Integer>> listOfEntries = new ArrayList<Map.Entry<String, Integer>>(entries);
        Collections.sort(listOfEntries, valueComparator);

        LinkedHashMap<String, Integer> sortedByValue = new LinkedHashMap<String, Integer>(listOfEntries.size());

        for(Map.Entry<String, Integer> entry : listOfEntries){

            sortedByValue.put(entry.getKey(), entry.getValue());
        }

        for(Map.Entry<String, Integer> entry : listOfEntries){

            sortedByValue.put(entry.getKey(), entry.getValue());
        }

        System.out.println("HashMap after sorting entries by values ");
        Set<Map.Entry<String, Integer>> entrySetSortedByValue = sortedByValue.entrySet();
        for(Map.Entry<String, Integer> mapping : entrySetSortedByValue){
            System.out.println(mapping.getKey() + " ==> " + mapping.getValue());
        }


    }

    static int lengthx(String a) {
        int count = 0;
        for (int j = 0; j < a.length(); j++) {
            if (a.charAt(j) == ' ') {
                count++;
            }
        }
        return count;
    }
}
Nocturno
  • 9,579
  • 5
  • 31
  • 39
Kumar Ayush
  • 103
  • 1
  • 4
0
import java.io.*;

class Linked {

    public static void main(String args[]) throws IOException {

        BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in));
        System.out.println("Enter the sentence");
        String st = br.readLine();
        st = st + " ";
        int a = lengthx(st);
        String arr[] = new String[a];
        int p = 0;
        int c = 0;

        for (int j = 0; j < st.length(); j++) {
            if (st.charAt(j) == ' ') {
                arr[p++] = st.substring(c,j);
                c = j + 1;
            }
        }
    }

    static int lengthx(String a) {
        int p = 0;
        for (int j = 0; j < a.length(); j++) {
            if (a.charAt(j) == ' ') {
                p++;
            }
        }
        return p;
    }
}
Vega
  • 27,856
  • 27
  • 95
  • 103
0

Simply use Java 8 Stream collectors groupby function:

    import java.util.function.Function;
    import java.util.stream.Collectors;  

    static String[] COUNTRY_NAMES 
  = { "China", "Australia", "India", "USA", "USSR", "UK", "China", 
  "France", "Poland", "Austria", "India", "USA", "Egypt", "China" };

    Map<String, Long> result = Stream.of(COUNTRY_NAMES).collect(
            Collectors.groupingBy(Function.identity(), Collectors.counting()));
Koenigsegg
  • 575
  • 1
  • 9
  • 23
0

Count frequency of elements of list in java 8

List<Integer> list = new ArrayList<Integer>();
Collections.addAll(list,3,6,3,8,4,9,3,6,9,4,8,3,7,2);
Map<Integer, Long> frequencyMap = list.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));

    System.out.println(frequencyMap);

Note : For String frequency counting split the string and convert it to list and use streams for count frequency => (Map frequencyMap)*

Check below link

Narayana
  • 11
  • 1
0
    String s[]=st.split(" ");
    String sf[]=new String[s.length];
    int count[]=new int[s.length];
    sf[0]=s[0];
    int j=1;
    count[0]=1;
    for(int i=1;i<s.length;i++)
    {
        int t=j-1;
        while(t>=0)
        {

            if(s[i].equals(sf[t]))
            {
                count[t]++;
                break;
            }
            t--;
        }
        if(t<0)
        {
            sf[j]=s[i];
            count[j]++;
            j++;
        }
    }
0

Created a simple easy to understand solution for this problem covers all test cases-

import java.util.HashMap;
import java.util.Map;

/*
 * Problem Statement - Count Frequency of each word in a given string, ignoring special characters and space 
 * Input 1 - "To be or Not to be"
 * Output 1 - to(2 times), be(2 times), or(1 time), not(1 time)
 * 
 * Input 2 -"Star 123 ### 123 star"
 * Output - Star(2 times), 123(2 times)
 */

public class FrequencyofWords {

    public static void main(String[] args) {
        String s1="To be or not **** to be! is all i ask for";
        fnFrequencyofWords(s1);
        
    }
    
    //-------Supporting Function-----------------
    static void fnFrequencyofWords(String s1) {
        //------- Convert String to proper format----
        s1=s1.replaceAll("[^A-Za-z0-9\\s]","");
        s1=s1.replaceAll(" +"," ");
        s1=s1.toLowerCase();
        
        //-------Create String to an array with words------
        String[] s2=s1.split(" ");
        System.out.println(s1);
            
        //-------- Create a HashMap to store each word and its count--
        Map <String , Integer> map=new HashMap<String, Integer>();
        for(int i=0;i<s2.length;i++) {
        
        if(map.containsKey(s2[i])) //---- Verify if Word Already Exits---
            {
                map.put(s2[i], 1+ map.get(s2[i])); //-- Increment value by 1 if word already exits--
            }
            else {
                map.put(s2[i], 1); // --- Add Word to map and set value as 1 if it does not exist in map--
            }
        }
        System.out.println(map); //--- Print the HashMap with Key, Value Pair-------
    }
}
Krishna Majgaonkar
  • 1,532
  • 14
  • 25
-1
public class WordFrequencyProblem {

    public static void main(String args[]){
        String s="the quick brown fox jumps fox fox over the lazy dog brown";
        String alreadyProcessedWords="";
        boolean isCount=false;
        String[] splitWord = s.split("\\s|\\.");
        for(int i=0;i<splitWord.length;i++){
            String word = splitWord[i];
            int count = 0;
            isCount=false;
            if(!alreadyProcessedWords.contains(word)){
                for(int j=0;j<splitWord.length;j++){
                        if(word.equals(splitWord[j])){
                            count++;
                            isCount = true;
                            alreadyProcessedWords=alreadyProcessedWords+word+" ";
                        }
                    }
            }
            if(isCount)
            System.out.println(word +"Present "+ count);
        }
    }

}
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • 2
    Welcome to Stack Overflow! While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Petter Friberg Mar 02 '16 at 17:12
-1
public class TestSplit {

    public static void main(String[] args) {
            String input="Find the repeated word which is repeated in this string";
            List<String> output= (List) Arrays.asList(input.split(" "));

            for(String str: output) {
                    int occurrences = Collections.frequency(output, str);
                    System.out.println("Occurence of " + str+ " is "+occurrences);
            }

            System.out.println(output);
    }

}
xenteros
  • 15,586
  • 12
  • 56
  • 91
-1

Please try these it may be help for you

    public static void main(String[] args) {
        String str1="I am indian , I am proud to be indian proud.";
        Map<String,Integer> map=findFrquenciesInString(str1);
        System.out.println(map);
    }

    private static Map<String,Integer> findFrquenciesInString(String str1) {
        String[] strArr=str1.split(" ");
        Map<String,Integer> map=new HashMap<>();
        for(int i=0;i<strArr.length;i++) {
            int count=1;
            for(int j=i+1;j<strArr.length;j++) {
                if(strArr[i].equals(strArr[j]) && strArr[i]!="-1") {
                    strArr[j]="-1";
                    count++;
                }
            }
            if(count>1 && strArr[i]!="-1") {
                map.put(strArr[i], count);
                strArr[i]="-1";
            }
        }
        return map;
    }
hitesh bariya
  • 97
  • 1
  • 5
-1

try this

public void count()throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enetr the strring");
String s = in.readLine();
int l = s.length();
int a=0,b=0,c=0,i,j,y=0;
char d;
String x;
String n[] = new String [50];
int m[] = new int [50];
for (i=0;i<50;i++)
{
    m[i]=0;
}
for (i=0;i<l;i++)
{
    d = s.charAt(i);
    if((d==' ')||(d=='.'))
    {
        x = s.substring(a,i);
        a= i+1;
        for(j=0;j<b;j++)
        {
            if(x.equalsIgnoreCase(n[j]) == true)
            {
            m[j]++;
            c = 1;
            }
        }
        if(c==0)
        {
            n[b] = x;
            m[b] = 1;
            b++;
        }
    }
    c=0;
}
for(i=0;i<b;i++)
{
    for (j=0;j<b;j++)
    {
        if(y<m[j])
        {
            y=m[j];
        }
    }
    if(m[i]==y)
    {
        System.out.println(n[i] + " : " + m[i]);
        m[i]=0;
    }
    y=0;
}
}