Thursday, July 9, 2015

Java programs :http://www.java2novice.com/java-interview-programs/duplicate-string-character-count/

1.   

Count number of each char in a String

String str = "Hello World";
int len = str.length();
Map<Character, Integer> numChars = new HashMap<Character, Integer>(Math.min(len, 26));

for (int i = 0; i < len; ++i)
{
    char charAt = str.charAt(i);

    if (!numChars.containsKey(charAt))
    {
        numChars.put(charAt, 1);
    }
    else
    {
        numChars.put(charAt, numChars.get(charAt) + 1);
    }
}

System.out.println(numChars);

Java program to count occurrences of a character in String

How to find number of occurence of character or substring in String in JavaIn this Java program we will see couple of ways to count, how many times a particular character is present in String. First we we will see Spring framework’s StringUtils class and its static method countOccurrenceOf(String, character) which takes a String and character and returns occurrence of character into that String. After that we will see Apache commons StringUtils class for counting occurrence of a character in String. Apache commons StringUtils provide countMatches() method which can be used to count occurrence of one character or substring. Finally we will see most simple way of counting character using standard for loop and Java 5 advanced for loop. This solution can be extended not just for finding occurrence of character but also finding occurrences of substring.
import org.springframework.util.StringUtils;
/**  * Java program to count number of occurrence of any character on String.  * @author Javin Paul  */ public class CountCharacters {     public static void main(String args[]) {                    String input = "Today is Monday"; //count number of "a" on this String.                //Using Spring framework StringUtils class for finding occurrence of another String         int count = StringUtils.countOccurrencesOf(input, "a");                System.out.println("count of occurrence of character 'a' on String: " +
                " Today is Monday' using Spring StringUtils " + count);                //Using Apache commons lang StringUtils class         int number = org.apache.commons.lang.StringUtils.countMatches(input, "a");         System.out.println("count of character 'a' on String: 'Today is Monday' using commons StringUtils " + number);                //counting occurrence of character with loop         int charCount = 0;         for(int i =0 ; i<input.length(); i++){             if(input.charAt(i) == 'a'){                 charCount++;             }         }         System.out.println("count of character 'a' on String: 'Today is Monday' using for loop  " + charCount);                //a more elegant way of counting occurrence of character in String using foreach loop                charCount = 0; //resetting character count         for(char ch: input.toCharArray()){             if(ch == 'a'){                 charCount++;             }         }              System.out.println("count of character 'a' on String: 'Today is Monday' using for each loop  " + charCount);     }             } Output count of occurrence of character 'a' on String'Today is Monday' using Spring StringUtils 2 count of character 'a' on String'Today is Monday' using commons StringUtils 2 count of character 'a' on String'Today is Monday' using for loop  2 count of character 'a' on String'Today is Monday' using for each loop  2
Well beauty of this questions is that Interviewer can twist it on many ways, they can ask you to write a recursive function to count occurrences of a particular character or they can even ask to count how many times each characters has appear. So if a String contains multiple characters and you need to store count of each character, consider using HashMap for storing character as key and number of occurrence as value. Though there are other ways of doing it as well but I like the HashMap way of counting character for simplicity.
Other programming exercise for Java programmer

2 comments :

Anonymous said...
Collections.frequency() method might be useful in this case.
tejya gore said...
public class test  { public static void main(String[] args) {  String str = "ababccbdde"; int i,j,k; char[] ch = str.toCharArray(); int len=ch.length; for ( i = 0; i < len; i++) { int counter = 0; char c=str.charAt(i); for ( j = 0; j < len; j++) { if (c==ch[j]){ counter++; ch[j]='\u0000'; } } if(counter>0)System.out.println(c+"="+counter); } } }
Read more: http://javarevisited.blogspot.com/2012/12/how-to-count-occurrence-of-character-in-String.html#ixzz3fOO3T6st

1 comment: