Improve the logic using Erase-Remove Idiom.