Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save royki/9a4ac796fb66efbc8fc4041d30a8d829 to your computer and use it in GitHub Desktop.
Save royki/9a4ac796fb66efbc8fc4041d30a8d829 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