Skip to content

Instantly share code, notes, and snippets.

@feliperazeek
Created September 17, 2015 06:30
Show Gist options
  • Save feliperazeek/0cfce4a1e17d40513af7 to your computer and use it in GitHub Desktop.
Save feliperazeek/0cfce4a1e17d40513af7 to your computer and use it in GitHub Desktop.
Codility Missing Integer in Scala
object Solution {
def solution(A: Array[Int]): Int = {
val bitz = new java.util.BitSet(A.size)
val n = A.foldLeft(0) { (total, i) =>
if (i > 0 && i <= A.size && !bitz.get(i)) {
bitz.set(i)
total + 1
} else total
}
val possibilities = if (n < 1) 1
else n
(1 to possibilities).foldLeft(possibilities + 1) { (current, i) =>
bitz.get(i) match {
case true =>
current
case false =>
return i
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment