August 26, 2024

P08 - Eliminate consecutive duplicates of list elements.

If a list contains repeated elements, they should be replaced with a single copy of the element. The order of the elements should not be changed. Example:

> compress("aaaabccaadeeee".toList())
[a, b, c, a, d, e]

kotlin

package org.kotlin99.lists

import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import org.junit.Test

fun <T> compress(list: List<T>) =
    list.fold(emptyList<T>()) { result, value ->
        if (result.isNotEmpty() && result.last() == value) result
        else result + value
    }

class P08Test {
    @Test fun `eliminate consecutive duplicates of list elements`() {
        assertThat(compress("".toList()), equalTo("".toList()))
        assertThat(compress("abc".toList()), equalTo("abc".toList()))
        assertThat(compress("aaa".toList()), equalTo("a".toList()))
        assertThat(compress("aaaabccaadeeee".toList()), equalTo("abcade".toList()))
    }
}
Be first to comment
Leave a reply