0

What is the most efficient implementation of finding counts of each distinct element in a Scala array?

1 Answer 1

3

you can do it like this:

val xs = Array("a", "b", "c", "c", "a", "b", "c", "b", "b", "a")
xs.groupBy(identity).mapValues(_.length)

or like this:

xs.foldLeft(Map[String, Int]().withDefaultValue(0))((acc, x) => acc + (x -> (acc(x) + 1)))

or you could use internal mutability to prevent copying if you want to be more efficient:

def countElems[A](xs: Array[A]): Map[A, Int] = {
  val result = collection.mutable.Map[A, Int]().withDefaultValue(0)
  xs foreach { x => result += (x -> (result(x) + 1)) }
  result.toMap // this copies and makes it immutable, O(number of distinct elements)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Could you write it with xs parameter of type DataFrame (one column) without using collect() ? I failed in using foreach on a DataFrame and updating the result Map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.