Scala check if List contains slice of element of other List

10,737

Solution 1

Fastest for whom? For you or the CPU?

scala> val a = List("abc","def","ghi") ; val b = List("xy", "yz", "ef")
a: List[String] = List(abc, def, ghi)
b: List[String] = List(xy, yz, ef)

scala> b exists (s => a exists (_ contains s))
res0: Boolean = true

scala> val a = List("abc","def","ghi") ; val b = List("xy", "yz")
a: List[String] = List(abc, def, ghi)
b: List[String] = List(xy, yz)

scala> b exists (s => a exists (_ contains s))
res1: Boolean = false

Solution 2

I think the shortest way is: ListA.exists{ListB.contains}

Share:
10,737
Pius Friesch
Author by

Pius Friesch

Updated on June 04, 2022

Comments

  • Pius Friesch
    Pius Friesch almost 2 years
    A : List[String] 
    B : List[String]
    

    I want to know if any element of List B is a slice of any element of list A.

    Whats the fastest way to check it?