0% found this document useful (0 votes)
2 views

FrequencyQuestionJava

The audio discusses a Java program that calculates the frequency of characters in a given string, a common interview question. The presenter explains the logic behind the code, which uses a map to store character counts while ignoring spaces. The program is demonstrated with debugging to show how it processes the input and outputs the frequency of each character.

Uploaded by

Andrei Tapia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

FrequencyQuestionJava

The audio discusses a Java program that calculates the frequency of characters in a given string, a common interview question. The presenter explains the logic behind the code, which uses a map to store character counts while ignoring spaces. The program is demonstrated with debugging to show how it processes the input and outputs the frequency of each character.

Uploaded by

Andrei Tapia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Audio file

downloaded_audio_Java program to find Frequency of all characters in a string [Most


asked programming question].mp3
Transcript
00:00:01
Hello everyone so I'm Mansi from Java techies today I will be sharing with you one
of the most commonly asked questions that I have been experiencing in the past few
interviews that I have with various organizations. So I have just written down the
code before itself, I will just.
00:00:22
Show you the logic and the way to debug the code and justice to show you how.
00:00:29
To solve this particular problem, so the question here is that the most of most of
the people asked to find the frequency of all the letters that are there into a
particular string. For example, if it is like a BA occurred 2 times, so a a
frequency is 2 and here B.
00:00:50
Frequency is 2, so I have written the code for the same so I will just walk you
through first the code and then I will try to on the debug mode try to explain my
changes and then show you the output.
00:01:05
So here you can see I've created a class frequency of characters and string and
this is my main method. I am taking the input from the user using scanner and then
my entire logic is written into check frequency method into which I am passing the
string that is coming from the user. So this is my.
00:01:24
Check frequency method and I am taking a string from here and first of all
converting in it into character array and then what I'm doing is I'm simply
creating a map so my entire logic is actually based on map that is of a character
and integer as you can see here.
00:01:41
So character will have all the characters that are there into the string as part of
key in the map and the value will be there count which will be integer. So here
what I am doing is the entire I am iterating over the entire character array 1 by 1
and I'm checking if this.
00:02:02
Character is a letter, then only I'm proceeding further. Suppose you have spaces
into your string and you do not want the count of spaces. Because of that I have
checked is letter over here. If you want you can ignore that based on the question
of your.
00:02:18
Interviewer basically so here what I am doing is for the first time when every for
the first time there will be some character it will just check if map has contains
that particular character as the key it will go into the if block if not it will go
to the else. So for the first time it will land.
00:02:38
The else block and what it will do is it will just put the character along with the
initial count as one.
00:02:46
So suppose it encountered the character again. So then in that case it will go into
this if block, because that key will already be there, so you can see then what I
am doing is in the map I am just putting that particular character and the initial
frequency. I am just getting it using this get method and.
00:03:06
And incrementing one into it. So like this the frequency will get updated at the
end using a simple for loop. I am just printing my my map keys and values so I will
just debug this code so that you can understand in a better way.
00:03:32
I will just provide it a string.
00:03:43
String and just pressing enter. Now you can see.
00:03:48
A map is created initially with size 0 and this is my input character array. I then
on the 1st place it is a space, then it's a M whatever input I provided, the entire
thing is there. So I have a total of 40 characters into my string.
00:04:05
So let's just go over 1 by 1. So the first character is I.
00:04:11
So as I mentioned, it will not be initially in the map, so it will land to the else
block.
00:04:18
Right. And so it it landed to this block and initial count was updated as one. Now
the second one is actually a space. So here I have put a check of character dot IS
letter. This method returns false if it is not.
00:04:34
A letter basically that that ranges from A-Z, something like that.
00:04:39
So for space it will return false. That means it this this space will be ignored by
my code. If you want you want to include spaces count as well, you can simply
remove this check if you want that that is based on your requirement. So yeah so it
didn't. You can see the.
00:04:59
Map map has just one character, that is I and one with count as one.
00:05:08
Now the next one is a so you can see again it will lend it to the else block
because a was not there in my map initially A is again updated to B1 over here. Now
let's go to the next one.
00:05:22
Next one is M. Again, it will be updated. You can see here in the map it is getting
deflected.
00:05:31
Now it's again a space that will be again ignored.
00:05:36
Then I go to another one.
00:05:38
It's.
00:05:39
F and it also got updated with initial count as one.
00:05:43
Let's move forward.
00:05:46
R is also getting updated.
00:05:48
Now again, always there and now you can see I have M already as one of my key right
with the count as one and now I again got M so I delete should go to the if block
and update the count as two here.
00:06:06
So if I go there.
00:06:09
You can see it went to the if block and M got updated. Now I'll just simply run
this program. Likewise this entire thing will happen and you will get the output.
00:06:23
Yeah. So let me, let me go to the console. So this is my output so you can see a
occurred 4 * C was 2 * D was two times like where is this you can see all the
characters with their respective count.
00:06:37
So I think that's it from my end. I hope you like the video and you were able to
get some knowledge out of it.
00:06:48
Please do like, share and comment if you have some questions you can definitely ask
me I'll I'm happy to answer all of them and I will also share this piece of code
along with the.
00:07:00
The video thanks. Thanks a lot everyone. Thanks for watching. Bye.

You might also like