For a specific application I need to add integers from a non-sorted array to an empty sortedSet using a for loop. (Or to a new array which is sorted). I know this can be done avoiding loops but I want to use a loop in this instance.
This code seems broadly right:
def minFor(r: Array[Int]): Int = {
var itsSorted = collection.SortedSet.empty[Int]
for(i <- 0 to r.length)
itsSorted = itsSorted + i
}
But no matter how I tweak it I always end up with a mismatch error:
error: type mismatch;
found : Unit
required: Int
for(i <- 0 to r.length)
How do I return a sorted array or set via a loop?