5 Replies
looks like you do it right here
repeatCount = 0;
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)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
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 characterswoah