Say the number (as a string variable S) is '112991'.
Fist we find the count for '1': Set Count to 0, S[1] is '1', so increase Count to 1, S[2] is '1', increase Count to 2, S[3] S[4] and S[5] are not '1', so do nothing, finally S[5] is '1', so increase Count to 3, which is the final answer, because there are no more characters left.
Now I can do the same for '1' to '9' (and of course for '0').
As you see, I do not need to delete any character in S as we go.
(Delete is a relatively slow operation, looping over the string is fast.)
You could even have a function that returns all counts in one go:
- The function will return an array (defined as array['0'..'9'] of integer).
- Initialize the array to have all values to be 0
- Loop over the string and increment the proper array value
Just one loop over the string needed.
While it would be trivial to give you this code, I won't, since this is your homework.
Bart