C#C
C#10mo ago
Hamad

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;
        }
Was this page helpful?