+ 1

The most frequent character in a string

Anyone can describe this codes ? https://code.sololearn.com/ctGjPK8uotlM/#py Specially line 9 and line 19 - 21 ?

4th Jan 2018, 8:51 PM
NIMA
NIMA - avatar
1 Answer
+ 1
Hi! This code is just looking for which is the character that appears more times in a sting. For line 9, we still do not know even if the string is empty so initialize that the most common character is repeated 0 times. In the for loop, it goes through all characters an uses the method "count" to get how many times it appears in the string. The "if" conditional checks if the character that we are analyzing in this iteration appears more time than any analysed previously (or 0 if it is the first iteration since we initialised to 0 in the line 9) So if we go into the conditional means that we have a new candidate to be the most common in the sting and saves it. Hope this helps to you!
4th Jan 2018, 10:00 PM
Alfred CatalĂ