keep function

Keep first, last and peak occurences