The base logic isn't that bad, you walk through groups and write back.
Use a two-pointer technique: one pointer (read) scans the array to identify groups of consecutive identical characters, while the other pointer (write) writes the compressed output in place. For each group, write the character and, if the count is greater than 1, write the count as individual digits. Return the write pointer as the new length.
Pro tip: Emphasize that the count can be multi-digit, so you must convert it to characters digit by digit; also mention that the write pointer never overtakes the read pointer, ensuring constant space.
Set read and write pointers to 0. The read pointer will scan the original array, and the write pointer will track the position for the next compressed character.
While read < length, record the current character and count consecutive occurrences by advancing read until a different character is found.
Write the current character at the write pointer and increment write.
If the count is greater than 1, convert the count to a string (or extract digits) and write each digit at the write pointer, incrementing write for each digit.
After processing all groups, return the write pointer as the new length of the compressed array.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.