-
-
Save royki/9a4ac796fb66efbc8fc4041d30a8d829 to your computer and use it in GitHub Desktop.
Codility Missing Integer in Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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