Skip to content

Instantly share code, notes, and snippets.

@renanreismartins
Created February 21, 2023 12:31
Show Gist options
  • Save renanreismartins/473fb5c0517f3430de0a7afb547a9d05 to your computer and use it in GitHub Desktop.
Save renanreismartins/473fb5c0517f3430de0a7afb547a9d05 to your computer and use it in GitHub Desktop.
object MissingNumber extends App {
def missingNumber(nums: Array[Int]): Int = {
val found = new Array[Int](nums.length)
for (n <- nums) {
if (n < nums.length) found(n) = 1
}
val missing = found.indexOf(0)
if (missing == -1) nums.length else missing
}
println(missingNumber(Array(3, 0, 1)))
println(missingNumber(Array(0, 1)))
println(missingNumber(Array(9, 6, 4, 2, 3, 5, 7, 0, 1)))
println(missingNumber(Array(8, 6, 4, 2, 3, 5, 7, 0, 1)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment