Java Interview Question: How To Count Occurrences Of Each Character In String In Java

Java Interview Question: How To Count Occurrences Of Each Character In String In Java

Naveen AutomationLabs

2 года назад

84,163 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Sanghamitra Bhowmick Live
Sanghamitra Bhowmick Live - 02.08.2021 12:57

All my interest in Java is because of you. I can't thank you enough for all that you have done for us!! 👏

Ответить
kundan patil
kundan patil - 25.08.2023 06:33

The people who getting error in isBlank method they can use in that if statement like below
if(c!=' '){



}

Ответить
Anil DS
Anil DS - 16.05.2023 10:18

solution for counting space
int count=0;
for(char ch:name.toCharArray()) {
if(ch==' ') {
count++;
}
}
System.out.println("******************");
System.out.println(count);

Ответить
Anil DS
Anil DS - 16.05.2023 09:59

Solution for only space we need to take count of string and replaceall method
int n=name.length()-name.replaceAll(" ", "").length();
System.out.println("spaceCount");
System.out.println(n);

Ответить
Vivek Kumar
Vivek Kumar - 28.02.2023 19:15

Sir, how can i get output as te2s3h if input is teesssh with stream ??

Ответить
district_gamer
district_gamer - 28.11.2022 15:45

sir ap hindi main bnao yr video

Ответить
ThatsDeep
ThatsDeep - 05.11.2022 06:02

YOU'RE THE MAN!!, THANK YOU!!!!!!!!!!!! but why u had to test ur code for 10 min lol

Ответить
Paramjit Kaur
Paramjit Kaur - 18.10.2022 23:19

To count only spaces, we will remove NOT from condition like below
if(String.valueOf(c).isBlank())

Ответить
Parth Jadhav
Parth Jadhav - 11.10.2022 17:51

veryyy very nice explaination

Ответить
Ananth Vraj
Ananth Vraj - 10.10.2022 14:18

simple remove ! in (String.valueOf(C).isBlank()){} condtion

Ответить
13 @life
13 @life - 29.09.2022 09:59

Beautiful explanation

Ответить
WanderEatsTravel
WanderEatsTravel - 24.09.2022 13:42

Print occurences of each word and character in a string : it was asked in the interview

Ответить
SOMRAJ YADAV
SOMRAJ YADAV - 28.08.2022 18:33

Thank you Naveen!! God bless you 🙏

Ответить
Anant Pandey
Anant Pandey - 19.07.2022 19:10

Hi Naveen, I m getting error while using the isBlank() method. Error showing that method isBlank() is undefined for type string.

Ответить
programming hub bharat
programming hub bharat - 08.07.2022 18:51

Yes sir, by just removing the (!) In the
if(!string.valueof(c).isBlank()) we got only spaces

Ответить
Mohan
Mohan - 18.06.2022 19:56

String S1="hello"
String S2="12345"
Output=h1e2l3l4o5. Can you plz write program for above output

Ответить
Devanand Thirunavukkarasu
Devanand Thirunavukkarasu - 04.06.2022 13:25

sir. thanks for the video .can we do this in java 8

Ответить
sai kumar
sai kumar - 28.05.2022 09:45

Better, u can do videos for Less time

Ответить
codings skills.explore
codings skills.explore - 25.04.2022 12:07

1 2 three 3 4 three 5 6 three 7 8 three.......20.anyone can solve this please aisa output aana chahiye

Ответить
Surabhi B N
Surabhi B N - 16.01.2022 19:46

Your approach is really so easy to understand and you solve the program in most efficient way too
Thank you so much...

Ответить
Hari babu Velakavaram
Hari babu Velakavaram - 19.10.2021 10:45

Hi Naveen,
Just a question..

For thr first test case i.e input "test" the output Hashmap values must be {(t,1)(e,1)(s,1)(t,2)} .. right??
Because we are just adding a new key value pair to the hashmap but we are not updating the existing key-value pairs as per this code...
For the 4th input character : 't' it will check if 't' is already present in the map. Since it is already there, it will fetch it's value which is 1 and then increments it by 1..so it will become 2 and will update a new key-value pair as( t,2) to the existing hashmap...so the final hashmap output should be {(t,1)(e,1)(s,1)(t,2)} ..
Please explain how it is giving {(e,1)(s,1)(t,2)} as output..

Thanks in advance.

Ответить