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

preview_player
Показать описание
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 #javaprogramming #javainterviewquestionsandanswers

------------------------------------------------------------------------------------------------------------------------------------

Jumpstart your career with TalentSprint!

1.Take Subject wise Practice tests and All-India Mock Tests with Benchmarking, Feedback and Recommendations by Tia (A bot that has helped over 10,500 Students crack competitive exams).
2.Thrice a Week Problem-Solving Classes by Expert Trainer.
3.Trainer-Student Forum Access that provides a Highly interactive forum for instant doubt 4.Clearance & discussions with peers.
5.1 working Day Trainer Team Turnaround for Forum Queries.
-------------------------------------------------------------------------------------------------------------------------------------
Choose Your Preparation Method.
Online - Join and get a dashboard full of video lessons, ebooks, LIVE classes & other preparation material.
Pendrive - Delivered at your doorstep with a COD Option. A pocket classroom that helps you prepare on-the-go with no internet.
XP Centres - A classroom with a difference. This enhances your preparation and helps you explore more advanced forms.
-----------------------------------------------------------------------------------------------------------------------------------------

Fast forward your career with TalentSprint!

1.Prepare with India’s Leading Trainers
2.Study on the go with 24/7 Digital Learning
3.Practice with All India Tests
4.Improve with Personalized feedback with TIA
5.Get 1000+ IT recruitment drives per Year.
6.Win with 5 times higher success rate.

Рекомендации по теме
Комментарии
Автор

map in c++ very much easier than java code :

map<char, int> cnt;
string x = "AAXAA";

for(char c: x) {
cnt[c]++;
}

cout << cnt['A'] << endl;

cid
Автор

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

soudiptadutta
Автор

thank you very much, i was looking for something like this and you really good deed explain it

moiyetresorkonan
Автор

thanks for the video, you should check for and change the string to upper or lower case as well when removing the spaces

ishashri
Автор

if input= AAABBACCBB then output=3A2BA2C2B
what is the code for the above one

UdayKumar-olyz
Автор

Madam you did great
But by using for loop is very is to understand for non cs background and freshers so instead of dis make new one by using for loop
Thank you mam

veereshhiremath
Автор

use this solution....it uses collections to its fullest....


import java.awt.AWTException;

public class A {

public static void main(String[] args) {
String str = "helloworld";
List<String> myList = Arrays.asList(str.split(""));
Set<String> setx = new HashSet<String>(myList);
for(Object obj : setx) {
System.out.println(obj+" found in "+Collections.frequency(myList, obj));
}
}
}

shaantalk
Автор

at 6:20 you said youll show us how that looks in the main functions but you ventured off to something else.

Shiroyashasama
Автор

sorry but why you replacing character which is checked, you unnecessarily increase time complexity and making your code very harder !! please make it simpler !!

v.r.
Автор

If this explanation on a system, it would be better to understand.

shrikantkoli
Автор

@talentspirit can you explain countChar function with an example

akashagarwal
Автор

Mam can you please figure me out how to make replace function without using array???

k_thai_edits
Автор

Can we use for loop for that
Please reply

dhruvsingh
Автор

sach batu..?? kuch samaj nahi aaya..😕🙁

farazmohammed
Автор

u should have shown the exact code too.

lovishbhateja
Автор

use the HashMap and get rid of all the complex logic.

ankitshrivastava
Автор

The code doesn't work
package testCases;

public class OccurenceOfEachCharacter {

public static void occurence(String str, char ch) {
int count = 0;
while (str.indexOf(ch) != -1) {
count++;
str =

}
}

public static void main(String args[]) {
String str = "mycode";
while (str.length() > 0) {
char ch = str.charAt(0);
occurence(str, ch);
System.out.println(str);

str = str.replace("" + ch, "");
}
}
}

Sanjeevkumar-clnl
Автор

how to count occurance of char in a string without using any loop

saniyamansuri
Автор

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){
= "+count);
}
} //end of j loop

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

} //end of class

devendramalviya
Автор

1)write a java program to find the count of occurrences of each character in string but 

Example: I am ram

  A=2; time 

  B=0;

  C=0;

  .

  .

  .

  Z=0; times 

Pls explain

huvannacn