Last active
December 23, 2015 23:39
-
-
Save zh4ngx/6711203 to your computer and use it in GitHub Desktop.
Find missing number from array
This file contains hidden or 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
def missing_number num_array: | |
n = num_array.length + 1 | |
expected = n * (n + 1) / 2 | |
actual = num_array.reduce :+ | |
missing = expected - actual | |
return missing | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment