Ramesh Ramesh - 3 months ago 18
Scala Question

how to remove every nth element from the Scala list?

Is there any methods available to remove every

n th element
from the Scala
List
?

I hope we can do this inside
filter
method and return another list by writing a logic. But is this efficient way to do it?

Answer

Simply:

list.zipWithIndex
    .filter { case (_, i) => (i + 1) % n != 0 }
    .map { case (e, _) => e }