For a little something I was trying out in C++, I have accepted a string (say ‘a tomato is red’) and gotten rid of spaces (‘atomatoisred’).
Now how would I go about deleting recurring characters only, on condition that the first instance of that character gets to stay (so our example becomes,’atomisred’)?
Thanks in advance!
You can use the erase-remove idiom in conjunction with a set keeping track of the duplicate characters:
This also uses the fact that the return value of
std::set::insertis a pair whose second element is aboolindicating whether the insertion took place.