Shivansh
Shivansh

Reputation: 3544

Subtract one list from another in the same order

There are two list like this :

val listA=List("a","b","c","d")
    val listB=List("a","b")

I want the answer like List("c","d")

But if the order of listB changes then the output should be empty list like this :

val listA=List("a","b","c","d")
    val listB=List("a","c")

Then this should result in empty list.

Hence I want the result of ListA-ListB but the order should be taken care of !

Upvotes: 0

Views: 470

Answers (1)

jwvh
jwvh

Reputation: 51271

This appears to work, given your limited test cases.

def remainder[T](a: Seq[T], b: Seq[T]): Seq[T] = {
  val diff = a diff b
  if (a containsSlice diff) diff else Seq()
}

It might fall short with a more exacting and comprehensive set of test cases.

Upvotes: 4

Related Questions