Skip to main content

count frequency of characters in string using java

count frequency of characters in string using java 

In most of the interviews , we always get this question from interviewer :

program to count frequency of characters in string ?

program to count number of times character occurred in given String?

program to count number of times character repeated in statement ?

today we are going to see the solution for this , using java we will print count of frequency of characters in string :


import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class CountFrequency {
 public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		String input = null;
		int option=0;
		try {
			do {
				System.out.println("Enter 1 to test new String:  ");
				System.out.print("\nEnter 2 for exit : ");
				try {
				option = scn.nextInt();
				}
				catch(Exception e) {
					System.out.println("Wrong input ,Please Enter 1 or 2 ");
					e.printStackTrace();
					option=2;
				}
				
				if (option == 1) {
					System.out.println("Enter String here ->");
					input = scn.next();
					countNumberOfCharacters(input);
				}
				
				else if(option==2) {
					System.out.println("Program is stopped, run again to test");
				}
				else {
					System.out.println("Enter either 1 or 2");
				}
			} while (option != 2);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			scn.close();
		}
	}

	private static void countNumberOfCharacters(String input) {
		char[] myArray = input.toCharArray();
		Map<Character, Integer> myMap = new HashMap<Character, Integer>();
		for (int i = 0; i < myArray.length; i++) {
			int frequency = 0;
			char tempKey = myArray[i];
			if (!myMap.containsKey(tempKey)) {
				myMap.put(tempKey, frequency + 1);
			} else {
				myMap.put(tempKey, myMap.get(tempKey) + 1);
			}
		}
		System.out.println("Count is as below :");
		for (Entry<Character, Integer> myEntry : myMap.entrySet()) {
			System.out.println( myEntry.getKey() + ": " + myEntry.getValue());
		}
	}

}

Output :
Enter 1 to test new String:  

Enter 2 for exit : 1
Enter String here ->
crtr4u.com
Count is as below :
r: 2
c: 2
t: 1
4: 1
u: 1
m: 1
.: 1
o: 1
Enter 1 to test new String:  

Enter 2 for exit : 1
Enter String here ->
aaBBcccd
Count is as below :
a: 2
B: 2
c: 3
d: 1
Enter 1 to test new String:  

Enter 2 for exit : 2
Program is stopped, run again to test
Happy Learning, Keep Coding. count frequency of characters in string using java program to count frequency of characters in string using java program for counting frequency of characters in string using java count number of repeated character in String with occurences of all count frequency of characters in string using java program to count frequency of characters in string using java program for counting frequency of characters in string using java count number of repeated character in String with occurences of all count frequency of characters in string using java program to count frequency of characters in string using java program for counting frequency of characters in string using java count number of repeated character in String with occurences of all count frequency of characters in string using java program to count frequency of characters in string using java program for counting frequency of characters in string using java count number of repeated character in String with occurences of all count frequency of characters in string using java program to count frequency of characters in string using java program for counting frequency of characters in string using java count number of repeated character in String with occurences of all count frequency of characters in string using java program to count frequency of characters in string using java program for counting frequency of characters in string using java count number of repeated character in String with occurences of all count frequency of characters in string using java program to count frequency of characters in string using java program for counting frequency of characters in string using java count number of repeated character in String with occurences of all count frequency of characters in string using java program to count frequency of characters in string using java program for counting frequency of characters in string using java count number of repeated character in String with occurences of all

Popular Posts

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Today we will see solution for Error In Spring Boot : ***************************  APPLICATION FAILED TO START ***************************  Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.  Reason: Failed to determine a suitable driver class. --->My Application.yml : server: port: 8081 spring: datasource: driver-class-name: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@localhost:1521:xe username: sys as sysdba password: root Oracle Version is 21 C Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production Version 21.3.0.0.0. check my pom.xml here : <? xml version = " 1.0 " encoding = " UTF-8 " ?> < project xmlns = " http://maven.apache.org/POM/4.0.0 " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation = " http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/m...

Stream In Java

stream in java 8 : Stream API is used to process collection of objects. stream() method is present in Collection interface from package java.util . Stream is an interface present in java.util package, filter() and map() methods are present in Stream interface. -->when to use filter()  method ? If we want to filter elements from collection based on some boolean condition then we can use filter() method.  for example  : filtering even or odd numbers from list of numbers. -->When to use map()  method ? If we want to create new object for every object in collection then we should use map() method. for example : if we want to increase every number in list by 2 , here every object is getting modified ,we have to use map() in this type of scenario. In below example , we used both filter() and map() methods on stream , inside filter method we applied some condition and in map method we are transforming our object to different one. package com.crtr4u.www; import java...

How much Software Engineer Earn in India

  No limit of earning for free lancer software developer : In service based companies - If Software Engineer work for same MNC from beginning of career then sometimes financial growth is low,still they can reach in between 7 lacs to 15 lacs in India. If programmer switch job after every 2-3 year and work for at least 5 years then they can earn minimum in between 14 lacs to 25 lacs for the position of Senior Software Engineer.  Being from a normal college , completed my education with average percentage , then  As 2 year experienced guy - I was getting offers of 8 lacs, 10 lacs  As 5 years experienced guy the offers are like  Minimum 12 lacs ,14 lacs, 18 lacs from Indian MNCs.  Money is  no bar for right candidate, if you are able to crack interview and if you can  write effective code then you can earn more than 20 lacs in India itself. Be motivated, improve your technical skills and yes you can earn great amount from Job too...  People with ...