Q:

Counting how many times any of the items appear in a Scala List

I have the following list:
val list = List("this", "this", "that", "there", "here", "their", "where")

I want to count how many times "this" OR "that" appears. I can do something like:
 list.count(_ == "this") + list.count(_ == "that")

Is there most concise way of doing this?

A:

You can count more than one occurrence at a time. No need to call count twice.
scala> list.count(x => x == "this" || x == "that")
res4: Int = 3

A:

scala> list.count(Set("this", "that").contains)
res12: Int = 3

it's shorter
it's one-pass

If you need to count words in several different places using the same big list:
val m = list.groupBy(identity).mapValues(_.size).withDefaultValue(0)

will give you a handy Map with all counts, so you could do
scala> m("this") + m("that")
res11: Int = 3

