Efficient way to convert Scala Array to Unique Sorted List

前端 未结 7 1884
长发绾君心
长发绾君心 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:52

    I haven't measured, but I'm with Duncan, sort in place then use something like:

    util.Sorting.quickSort(array)
    array.foldRight(List.empty[Int]){ 
      case (a, b) => 
        if (!b.isEmpty && b(0) == a) 
          b 
        else 
          a :: b 
    }
    

    In theory this should be pretty efficient.

提交回复
热议问题