Scala turn comma delimited string to Array
scala> "1,100,53,5000,23,3,3,4,5,5".split(",").map(_.toInt).distinct
res1: Array[Int] = Array(1, 100, 53, 5000, 23, 3, 4, 5)
Obviously this raises an exception if one of the value in the array isn't an integer.
edit: Hadn't seen the 'distinct numbers only' part, fixed my answer.
Another version that deals nicely with non parseable values and just ignores them.
scala> "1,100,53,5000,will-not-fail,23,3,3,4,5,5".split(",").flatMap(maybeInt =>
scala.util.Try(maybeInt.toInt).toOption).distinct
res2: Array[Int] = Array(1, 100, 53, 5000, 23, 3, 4, 5)
added type checking for the string being parseable as Int :
package load.data
object SplitArray {
def splitArrayintoString(s: String): Set[Int] =
{
val strArray = s.split(",")
strArray filter isParseAbleAsInt map (_.toInt) toSet
}
private def isParseAbleAsInt(str: String): Boolean =
str.forall(Character.isDigit(_))
}