Solution for compressing a list with repeated elements