Scala : Remove consecutive duplicates from List

object scalaprolog extends App { var list2=List(1,2,2,2,3,3,4,5,6,7,7,8,7,2,2) def duplicate(l:List[Int]):List[Int]= { var testlist=scala.collection.mutable.ListBuffer.empty[Int] testlist += l(0) //testlist.toList def loop(lis:List[Int],n:Int):List[Int]= { if(n<list2.length) { //println(n) if (testlist.last != lis(n)) {println(testlist.last,lis(n)) testlist +=lis(n) //loop(lis,n+1) } loop(lis,n+1) } testlist.toList } loop(list2,0) testlist.toList } println(duplicate(list2) ) }       Output :   (1,2) (2,3) (3,4) (4,5) (5,6) (6,7) (7,8) (8,7)… Read More Scala : Remove consecutive duplicates from List

Scala:Filter Positions in a List

Hacker Rank –   For a given list with N integers, return a new list removing the elements at odd positions. The input and output portions will be handled automatically. You need to write a function with the recommended method signature. Input Format N integers contained in the list, each on a separate line. Output Format… Read More Scala:Filter Positions in a List

Scala : List data structure element removal

Objective –   Implement the function tail for removing the first element of a List. Solution –  object patternmatcing extends App { sealed trait List[+A] // deifine trait list with sealed i.e all functions ust be declared here case object Nil extends List[Nothing] // case object case class Cons[+A](head: A, tail: List[A]) extends List[A] // case… Read More Scala : List data structure element removal

Scala : Check Sort using anonymous function

object issorted extends App{ def issorted[A](as:Array[A],ordered:(A,A)=>Boolean):Boolean= { def loop(n:Int): Boolean = { if (n<=0) true // if recursion goes to first element then array is sorted else ordered(as(n),as(n-1)) && loop(n-1) // else check the and && conditions for oredred function and loop(n-1) .. bith true then true else false } loop(as.length-1) } print(issorted(Array(1,2,3,4,5),(x:Int,y:Int)=>x>y)) // declare… Read More Scala : Check Sort using anonymous function