[codility]Distinct

662 ワード

最近scala言語を勉強していますが、scalaコードは以下の通りです.
import scala.collection.JavaConversions._

object Solution {
    def solution(A: Array[Int]): Int = {
        // write your code in Scala 2.10
        // using quick sort to sort the array first
        scala.util.Sorting.quickSort(A)
        // assign the initial former unique element to be the out of range value
        var formerValue: Int = -10000000
        var uniqueCnt: Int = 0
        
        for(curValue <- A) {
            if(curValue != formerValue) {
                formerValue = curValue
                uniqueCnt += 1
            }
        }
        uniqueCnt
    }
}