Word count duplicated in Java, it counts character only count word duplicated

245 views Asked by At

My question is word count duplicated in Java count duplicates word it count character only enter code here.

I am use core Java program only

public static void getCount(String name) {
    Map<Character, Integer> names = new HashMap<Character, Integer>();
    for(int i = 0; i < name.length(); i++) {

       char c = name.charAt(i);
       Integer count = names.get(c);
       System.out.println(names.get(c));
       System.out.println("the count"+count);
       if (count == null) {
           count = 0;
       }
       names.put(c, count + 1);
       System.out.println("111111111111");}`
   Set<Character> a = names.keySet();
   for (Character t : a) {
       System.out.println(t + " Ocurred " + names.get(t) + " times");
   }
3

There are 3 answers

0
griFlo On

i think this may help you

    String name = "banana";
    Map<Character, Integer> countMap = new HashMap<>();
    name.chars()
.forEach((int i) -> countMap.put((char) i, countMap.getOrDefault((char) i, 0) + 1));

countMap.forEach((Character c,Integer count)->System.out.println("Character: "+c+" count: "+count));

or (I think this will have a better performance)

Map<Integer, Long> countMap2 = name.chars().boxed().collect(Collectors.groupingBy(Integer::intValue, Collectors.counting()));
countMap2.forEach((Integer c, Long count) -> {
        if (count > 1) {
            System.out.println("Character: " + (char) c.intValue() + " count: " + count);
        }
    });
0
Gopal Kk On

If the query is for counting the duplicate words in a given string, this is the code:

public static void getCount(String name) {
                java.util.StringTokenizer stoken = new java.util.StringTokenizer(name, " ");
                boolean flag = true;
                if (stoken.countTokens() > 1) {
                    java.util.Map<String, Integer> wordCountMap = new java.util.HashMap<String, Integer>();
                    while (stoken.hasMoreElements()) {
                        String str = stoken.nextElement().toString();
                        if (wordCountMap.containsKey(str)) {
                            wordCountMap.put(str, wordCountMap.get(str) + 1);
                        } else {
                            wordCountMap.put(str, 1);
                        }
                    }
                    System.out.println("Checking for Duplicates..");
                    for (String values : wordCountMap.keySet()) {
                        if (wordCountMap.get(values) > 1) {
                            flag = false;
                            System.out.println(values + "\t\t["+ wordCountMap.get(values) + "]");
                        }
                    }
                }
                if (flag) {
                    System.out.println("No duplicate words");
                }
            }
0
Minnie On

The program inputs a sentence and tell returns the highest frequency duplicate word. Please test this code rigorously.

import java.util.*;
public class DupsWords {

    public static String countDupsWords(String[] arr){
        Hashtable<String,Integer> ht = new Hashtable<String,Integer>(); 
        for(int i=0;i<arr.length;i++){
            if(ht.containsKey(arr[i])){
                ht.put(arr[i],ht.get(arr[i])+1);
            } else{
                ht.put(arr[i],1);
            }       
        }   
        Set<String> keys=ht.keySet();
        String result=null;
        int max=0;
        for(String itr : keys){
            if(Integer.parseInt(ht.get(itr).toString())>max){
                max=Integer.parseInt(ht.get(itr).toString());
                if((ht.contains(max))&&(max>1)){
                    result=itr;
                }       
            }               
        }
        if(result ==null){
            return "No Duplicate";
        }
        else{
            System.out.print("count is "+ max  +" for ");
            return ("'"+ result +"'" + " as a duplicate word");
        }
    }   

    public static void main(String args[ ]){
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the String");
        String s= scan.nextLine();      
        String[] arr=s.split(" ");
        System.out.print(countDupsWords(arr));
    }

}