Welcome back.
In this video we're going to find out which condition is the most common condition because we have four
different conditions sunny, rainy, cloudy and snowy.
And now we want to know which happened the most times.
And therefore I'm going to create a new method that will return a string.
So it's static again because otherwise I cannot call it inside of my main method.
You can try it.
It won't work.
I'm going to call this one most common condition.
And I'm going to pass all conditions in there.
So conditions.
So now this obviously needs to return something.
But before we return I'm going to create a variable that will count how often a given condition has
been the case.
And I'm going to store the most common condition as well which will be conditions at position zero at
the start.
So I can just assume that the first entry inside of my conditions will be the most common, because
at the beginning, it is only once I start comparing it to the others, I can find out whether maybe
another one is more common.
So here what I'm going to do is I'm going to use a for loop and for int zero.
And here I'm going to say conditions dot length.
So I go through all conditions through every single one of them.
And I'm going to have another variable here which will be a temporary count that is only available within
this for loop.
So I just need to know for the current condition how often did you appear.
And then I need to compare that to any other count.
So if the current condition has happened more times than any other condition, then I will just know,
okay, this condition is the most common and that's why I need to compare the count with the temp count.
This will make a lot more sense once we actually have the code down.
So let's go ahead and keep going.
So I'm going to have another for loop which will go through my conditions with a J this time.
So this is a for loop within a for loop.
And we use J instead of I this time so that we know that we are currently in the next iteration.
So this second for loop will also go through the conditions.
And why is that?
Well the thing is we go through all conditions, but then we go through all conditions within each iteration
of each condition once again.
So this means that we are using that to compare.
So if I just go ahead and say if conditions J is equal to conditions I, then I know that I need to
increment my temp count.
Okay.
So outside of here I can then check if my temp count is greater than my count.
If that's the case, then I know that count will be temp count and my most common condition will be
the I condition.
So then I can return my most common.
So what is going on here.
Because there's a lot happening.
So let's assume we have five conditions.
And the first condition that we go through is going to be okay.
So let's try to understand what is going on in here.
So what we're doing is with this for loop here we're going through all conditions.
And then we create this variable every single time for each iteration a new.
So for each condition we go through it step by step.
So let's say we have ten different weather conditions not the different types of conditions.
Not this here, but we have ten different days at which we have conditions.
So for every single day that we have a certain condition, we're checking that condition for thoroughly.
We're creating a temp count, which just is going to store how often that particular condition is inside
of that array.
So let's say in this iteration, in the first iteration, let's say the condition is sunny.
Okay.
So now we're checking with this for loop.
We're going through all conditions once again.
And we're checking is sunny the condition for the condition that we're currently looking at.
So if the first one was sunny we're now looking is the second one.
Also sunny is the third one.
Also sunny is the fourth one.
Also sunny.
And every time that one of those is sunny, we are incrementing temp count.
So now we're saying okay, yeah, actually sunny was there once and if sunny was there again then we
increment temp count again.
And then once we are done comparing the current condition to all other conditions.
So how often it was there then we check is the temp count.
So the temporary count for the current condition, for today's condition so to speak, is that bigger
or taller or more often?
Turn.
Then the other count that we set up as the global count, so to speak.
If that's the case, then we override the global count.
We override whatever the most amounts of a particular condition was with the current condition of which
we are in the for loop for.
And at the same time we say that the current condition.
So at the position of the for loops iterations, I is going to be the one that is most common.
So then we return most common.
I think this will make a little more sense if we run through this step by step.
Let me actually create a breakpoint in here.
And also let's run this line here.
So Console.WriteLine I'm going to say most common condition is and then or was.
If we follow the same structure that I had before for and then again we need the curly brackets.
And we call our most common condition method with our conditions.
And we need to pass the weather conditions here okay.
Most common condition is going to be weather conditions.
And now let's run this.
So let's simulate five times.
So we go in here and we set temp count to zero.
And so at this point our conditions are you can see here they are sunny rainy cloudy sunny and snowy.
So the one condition that happened the most times will be sunny.
And it is sunny at the first iteration.
So let's go through this step by step.
So we have the conditions.
It's checking is conditions at position zero.
Is it the same as conditions at position zero as well.
So is sunny equals sunny is what it's checking here.
So it says yeah it's true.
So this if statement is true.
So it goes into the if statement.
So it increments temp count by one.
So then we go to the next step.
We go for through this for loop.
Once again j is incremented by one.
So now it's going to check.
Do we still have something inside of our array.
Yes we do now it's checking.
Is the condition at position one the same as the condition is at position zero.
So we're checking basically is sunny equal rainy which is not true.
So it goes out of this for loop.
Let's run it again.
It's incrementing J again.
It's checking.
Do we still have items inside of our conditions array.
Yes we do.
So it goes into this if statements where it compares is the condition at position two where j currently
is j is two.
Is it the same as it is at one zero.
So it says is sunny equal cloudy?
No it's not.
So it gets out of this for loop.
So it checks again.
Do we still have items now it's comparing is j equals.
Let me see.
Three is j equals three.
So at the third position is it the same as it is at zero.
So that's what this check here does.
Yes it is.
So what it does is it increments temp count by one.
So let's go here.
And now we can see temp count is zero.
And in the next iteration temp count is is two.
So now we know that well it goes through the last iteration again through the fifth item.
So it's checking is the condition at position four.
So at index four is snowy equals sunny.
No it's not.
So it gets out of this if statement.
And it also gets out of this for loop because now there are no more items.
So it increments J by one.
It checks.
Are there more items.
It says no there are no more items.
So it goes to this if statement here where it checks temp count greater count where it checks is two
greater zero.
Yes it is.
So then it goes to the next line where it says okay, count which was zero before.
Now it's going to be overwritten with our new temp count which is two.
And at the same time, whatever was most common is now going to be whatever is the condition at zero,
because I is zero at this point and conditions at zero is sunny.
So we will now know that most common will be sunny.
So this was one iteration of our for loop of the our outer for loop.
Now it will go through every single other item in our conditions array okay.
So it will try that again.
Now it will increment I by one.
So it will check.
Do we still have items in the list.
Yes we do.
And now it will go through this entire process again.
It will create a variable int temp count zero.
It will check this for loop multiple times.
So now it will compare.
Here is the entry at zero.
The same as the entry at one.
So if we look at our conditions we can see is sunny equal rainy?
No it's not.
So it's going to get out of it.
And now it does it again and again.
So once it was true by the way.
Because if we look at it when one when it was one one, so where it said is rainy equals rainy.
Yes.
Rainy equals rainy.
So now it just compares every single time and it sees rainy only was there once.
So temp count at this point is going to be one.
Because rainy just was there once.
And yeah, that's when it jumped out of this iteration of the for loop.
And now does it for all other items that we have.
And we have cloudy, sunny and snowy.
So because sunny was there twice it will run through the sunny thing again.
It will see sunny is there twice.
So it will then end up making our sunny the most likely or the most common item.
All right, that's pretty much how we found out which item is the most common.
Now we can just step out of it and step out again.
And then look at our result here.
Most common condition is sunny.
All right.
So this was a little more complicated and a lot more going on here.
But we combined many concepts in one.
So you see we combined for loops with for loops with increments with if conditions with methods.
And we ended up with finding out which item is the most common inside of an array.
So that's it for this video.
Now obviously you could go further and say, okay, what if there are multiple items that have the same
count?
So you could go further.
You could make it more complicated where you just say, okay, well, what if sunny and cloudy are the
most common because both of them are there two times, for example.
Then you could extend that code, but that's something for you to do.
I wish you good luck with that and see you in the next video.