Problem 14
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package ninetyNineScalaProblems
|
||||
|
||||
import scala.annotation.tailrec
|
||||
|
||||
object Problem14:
|
||||
def duplicateElements(list: List[Any]): List[Any] =
|
||||
@tailrec
|
||||
def loop(cursor: List[Any], acc: List[Any]): List[Any] =
|
||||
cursor match
|
||||
case e :: tail =>
|
||||
loop(tail, e :: e :: acc)
|
||||
case Nil =>
|
||||
acc
|
||||
|
||||
loop(list, List()).reverse
|
||||
|
||||
def duplicateElements2(list: List[Any]): List[Any] =
|
||||
list.flatMap(i => i :: List(i))
|
||||
Reference in New Issue
Block a user