Skip to content

Instantly share code, notes, and snippets.

@Toscan0
Last active April 5, 2023 12:26
Show Gist options
  • Save Toscan0/6de685d4ff4a5981fcd7d7ff78bb68c4 to your computer and use it in GitHub Desktop.
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))
// 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