Java Program to Find the Count of Occurrences of Each Character in a String

Java

Video is ready, Click Here to View ×


Write a Java Program to Find the Count of Occurrences of Each Character in a String. Well, if you are appearing for any IT company interview this is the most expected question in your Technical round of interviews. Now, you need not fear for Technical rounds, ” .because we are bringing you the entire series of “Frequently asked Programming Questi”.Subscribe to our channel for more videos and Crack your Interviews successfully!

#frequentlyaskedprogammingquestions #javaprograms…

9 thoughts on “Java Program to Find the Count of Occurrences of Each Character in a String

  1. k j m E.g take b2 we will have k,j,j,m etc the same will have k,m,j,m then c will have k,k,j,m etc
    j m k take c2(b) we will have kjjm the same will have k,m,j,m then c will have kkjm etc
    j k m one letter k generates 3 options, the second the same when we get to row 2 the same
    m k j bearing in mind the letters the options generated by the column from 1 to 4 must differ
    j k m even if I what to go up to 100. which branch of maths is this?
    k m j from k alone in my example I will get three more columns, j the same getting these results, e.g
    m j k k again the same k to produce
    b2 k b2 k b2 k after k I will start with j,m ,j,m ,j,k,m
    b3 j c3 m d3 k the j k j m j k m.
    b4 j b4 j b4 j
    b5 m b5 m b5 m
    b6 j b6 j b6 j
    b7 k b7 k b7 k
    b8 m b8 m b8 m

  2. I think we should use the function map.getOrDefault here. By using that we don't have to write more lines of code. Here is the complete program.

    public class CountEachCharacterInString {
    public static void main(String[] args) {
    String str= "GinaGiniProtijayi";

    fix(str);
    }

    private static void fix(String a) {
    Map<Character , Integer> map = new LinkedHashMap<>();
    List<Character> list = new ArrayList<>();
    for( char ch : a.toCharArray()) {
    map.put(ch, map.getOrDefault(ch, 0) + 1 );

    }//for
    map.forEach((k,v) -> System.out.println(k +" : " + v));

    }

    }//end1

  3. second approach is good and i request that please uses collection approach like in this ques to do programming and alternatives will be add on benefit…thank you

  4. ma'am one more solution

    public class Test {

    public static void main(String[] args) {
    String ss = "you are beautiful";

    String s = ss.replace(" ", ""); //replacing the space
    for(int i =0; i<s.length();i++)
    {
    int count =0;
    for(int j=0; j<s.length() ; j++)
    {
    if(j<i && s.charAt(i)==s.charAt(j)){
    break;
    }

    if(s.charAt(i)==s.charAt(j)){
    count++;
    }
    if(j==s.length()-1){
    System.out.println(s.charAt(i)+" = "+count);
    }
    } //end of j loop

    } //end of i loop
    } //end of main method

    } //end of class

Leave a Reply

Your email address will not be published. Required fields are marked *