# scala - Scala 如何将列表元素与下一个元素进行比较，以获得这里元素？

``````
val ll = List(1, 2, 3, 6, 3, 7, 5, 5, 6, 3)

//Desired output: List(1, 2, 3, 6, 3, 7, 5, 6, 3)

``````

``````
times.zipWithIndex.collect

{

case (element, index)

//index+1 will be incremented out of my list

if (times(index)!= times(index+1)) => times(index)

}

``````

``````
times.zipWithIndex.collect

{

case (element, index)

if (index <times.size)

if (times(index)!= times(index+1)) => times(index)

}

``````

``````
val r = times.zip(times.drop(1)).withFilter(t => t._1!= t._2).map(_._1) :+ times.last

``````
``````
scala> val times = List(1, 2, 3, 6, 3, 7, 5, 5, 6, 3)

times: List[Int] = List(1, 2, 3, 6, 3, 7, 5, 5, 6, 3)

scala> val r = times.zip(times.drop(1)).withFilter(t => t._1!= t._2).map(_._1) :+ times.last

r: List[Int] = List(1, 2, 3, 6, 3, 7, 5, 6, 3)

``````

``````
ll.foldLeft(List[Int]())((acc, x) => acc match {case Nil => List(x) case y => if (y.last == x) y else y :+ x})

``````

``````
val ll = List(1, 2, 3, 6, 3, 7, 5, 5, 6, 3)

ll.sliding(2)

. filter( t => t.length> 1 && t(0)!= t(1) )

. map( t => t(0) )

. toList :+ ll.last

``````