Get lowest integer that is not in set Is there a better (functional or more idiomatic) way to get the first positive number that is not included in a set?

val set = Set(4, 1, 2, 0, 5)
var x = 0
while(set(x)) x += 1
println(x) // Should print 3
You could try for a more functional expression that matches problem description more closely:

 Stream.from(1).filter(!set.contains(_)).head


[Caveat: I'm not a scala programmer, I have no idea how idiomatic this is]