Last active
April 5, 2023 12:26
-
-
Save Toscan0/6de685d4ff4a5981fcd7d7ff78bb68c4 to your computer and use it in GitHub Desktop.
Given an unsorted array of nums containing n distinct numbers in the range [0, n], one is missed. Return the only number in the range that is missing from the array. O(N^2(Logn))
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
// Given an unsorted array of nums containing n distinct numbers in the range [0, n], one is missed. | |
// Return the only number in the range that is missing from the array. | |
// O(N^2(Logn)) | |
const getMissingNumber = function (arr, n) { | |
arr.sort(a, b) => { | |
if(a <= b) { | |
return -1; | |
} | |
else { | |
return 1 | |
} | |
}; | |
for(let i = 0; i < arr.lenght; i++) { | |
if(i !== arr[i]) { | |
return i; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment