Efficient way to convert Scala Array to Unique Sorted List

前端 未结 7 2046
长发绾君心
长发绾君心 2021-01-04 15:00

Can anybody optimize following statement in Scala:

// maybe large
val someArray = Array(9, 1, 6, 2, 1, 9, 4, 5, 1, 6, 5, 0, 6) 

// output a sorted list whic         


        
7条回答
  •  半阙折子戏
    2021-01-04 15:42

    For efficiency, depending on your value of large:

    val a = someArray.toSet.filter(_>0).toArray
    java.util.Arrays.sort(a) // quicksort, mutable data structures bad :-)
    res15: Array[Int] = Array(1, 2, 4, 5, 6, 9)
    

    Note that this does the sort using qsort on an unboxed array.

提交回复
热议问题