Hi! My question is not about removing duplicates from a collection. It’s about removing duplicates only when they occur in series in a collection.
For example:
[1,2,2,3,2,1]
→ [1,2,3,2,1]
["A","A","B","A","C","C","A","C"]
→ ["A","B","A","C","A","C"]
I want to do this in a recursive / functional way and have implemented this as so:
fun removeRepeats(list: List<Any>): List<Any>{
val last = if(list.size>2) removeRepeats(list.drop(1)) else list.takeLast(1)
val out = if(last.first() == list.first()) last else listOf(list.take(1),last) .flatten()
return out
}
I’m wondering if this was the best way to write this function recursively. I don’t know if the if statements are really necessary.
Thanks!