C
C#4d ago
M. Hammad

Unable to reset repeatCount variable to 0

static char MostFrequentCharFounder(string userInput)
{
List<char> chars = new List<char>();
int indexOfMostRepeated=0;
int repeatCount=0;
int highestRepeatCount = 0;
userInput.Trim();

foreach (char c in userInput.ToLower())
{
if (c != ' ')
{
chars.Add(c);
}
}
for (int i = 0; i <= chars.Count - 1; i++)
{
char currentElement = chars[i];
for (int j = 0; j <= chars.Count - 1; j++)
{
if (currentElement == chars[j])
{
repeatCount++;
}
}
if(repeatCount>highestRepeatCount)
{
highestRepeatCount = repeatCount;
repeatCount = 0;
indexOfMostRepeated = i;
}
}
char mostRepeated = chars[indexOfMostRepeated];
return mostRepeated;
}
static char MostFrequentCharFounder(string userInput)
{
List<char> chars = new List<char>();
int indexOfMostRepeated=0;
int repeatCount=0;
int highestRepeatCount = 0;
userInput.Trim();

foreach (char c in userInput.ToLower())
{
if (c != ' ')
{
chars.Add(c);
}
}
for (int i = 0; i <= chars.Count - 1; i++)
{
char currentElement = chars[i];
for (int j = 0; j <= chars.Count - 1; j++)
{
if (currentElement == chars[j])
{
repeatCount++;
}
}
if(repeatCount>highestRepeatCount)
{
highestRepeatCount = repeatCount;
repeatCount = 0;
indexOfMostRepeated = i;
}
}
char mostRepeated = chars[indexOfMostRepeated];
return mostRepeated;
}
5 Replies
Jimmacle
Jimmacle4d ago
looks like you do it right here repeatCount = 0;
tannergooding
tannergooding4d ago
this looks like an expensive way to do the check, first of all; there are much simpler ways to handle this you also have a bug in that you're miscounting by 1 because you're starting at 0 that is given your intent cat will find c = 1, a = 1, t = 1; so you're rather finding the occurrence count; not repeat count the actual issue is, however, that you're getting c = 1, a = 1, t = 2 because repeatCount isn't reset after a that is, repeatCount starts at 0 the first iteration you encounter c, you increment the count by 1 this is greater than highestRepeatCount which is 0 so you set highestRepeatCount = 1, indexOfMostRepeated = 0, and reset repeatCount = 0 then you encounter a, you unnecessarily check c again encounter a, increment the count by 1 this is not greater than the highest, so you don't modify repeatCount then you encounter t, you unnecessarily check ca again then you encounter t and increment the count by 1 (making it 2 since it wasn't reset after checking a) so to fix this, you want to always reset the repeatCount before the next iteration of the for (int i = ...) loop but you should really rename this occurrenceCount instead you should consider ways you can avoid duplicating work (avoid checking characters you've already checked, you can) you should consider if you can avoid allocating a List<char> at all (you can) and if you can avoid iterating the input more than once (you can)
M. Hammad
M. HammadOP4d ago
I understand now... I couldn't figure it out for an hour. mhmmm yes I actually count occurrence so I am doing that I understand that I am gonna fix repeatative checks btw how I can avoid allocating a List<char> I am also gonna look at the 4th point
tannergooding
tannergooding4d ago
string is already indexable, so you don't need to allocate a list at all that is, a string is already effectively a list of characters
M. Hammad
M. HammadOP4d ago
woah

Did you find this page helpful?