Miscellaneous problems · Exercise 82 of 100
Find the first unrepeated character
Count how often each character occurs in the whole string. Then read the original string from left to right and stop at the first character whose count is one. The second pass preserves the order in which the characters appeared.
Try it yourself
In the word peppercorn, which lowercase letter is the first to occur exactly once?
One-step hint
Tally all the letters in peppercorn before choosing one. Then reread the original word from the left, checking each letter's final tally.
Worked solution
- Count the letters. The letter p appears three times, e and r appear twice each, and c, o and n appear once each.
- Reading from the start, every letter in pepper has a count above one. The next letter is c, so c is the first with a count of one.