Created
February 21, 2023 12:31
-
-
Save renanreismartins/473fb5c0517f3430de0a7afb547a9d05 to your computer and use it in GitHub Desktop.
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 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