Friday, February 28, 2014

How to remove duplicate entries from a list in Scala

# Method 1 
val dirty = List("a", "b", "a", "c")
> disty.distinct   ('distinct' does not work on the Array elements)
res0: List[java.lang.String] = List(a, b, c)

# Method 2
val arr = Array(
  Array("1234","rjan","nilsson"),
  Array("4321","eva-lisa","nyman"),
  Array("1234","eva","nilsson")
)

arr.groupBy( _(0)).map{ case (_, vs) => vs.head}.toArray

No comments: