-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
|
||
// *** Guided approach 2: bitwise operations and avoids potential overflow issues with very large sums | ||
// XOR method | ||
// Time complexity: O(n)(two loops: one for numbers 0 to n and one for array elements) | ||
// Space complexity: O(1) | ||
|
||
var missingNumber = function (nums) { | ||
// XOR with itself results in 0 : a xor a = 0 | ||
// XOR with 0 results in the number itself : a xor 0 = a | ||
// XOR is commutative and associative | ||
|
||
const n = nums.length; | ||
|
||
let xor = 0; | ||
|
||
for (let i = 0; i <= n; i++) { | ||
xor ^= i; | ||
} | ||
|
||
for (any of nums) { | ||
xor ^= any; | ||
} | ||
|
||
return xor; | ||
}; | ||
|
||
// *** Guided approach 1: simplicity and clarity | ||
// Gauss' Formula (Sum of First n Numbers): n*(n+1) / 2 | ||
// Time complexity: O(n) | ||
// Space complexity: O(1) | ||
// var missingNumber = function (nums) { | ||
// const n = nums.length; | ||
// const expectedSum = (n * (n + 1)) / 2; | ||
// const actualSum = nums.reduce((acc, cur) => acc + cur, 0); // O(n) | ||
|
||
// const missingNum = expectedSum - actualSum; | ||
|
||
// return missingNum; | ||
// }; | ||
|
||
// *** My own approach | ||
// Time complexity: O(n^2) | ||
// Space complexity: O(n) | ||
// var missingNumber = function (nums) { | ||
|
||
// let distinctNums = new Set([]); | ||
|
||
// for (any of nums) { | ||
// if (distinctNums.has(any)) { | ||
// return | ||
// } else { | ||
// distinctNums.add(any) | ||
// } | ||
// } | ||
|
||
// const n = distinctNums.size; | ||
|
||
// for (let i = 0; i <= n; i++) { | ||
// if (!nums.includes(i)) { | ||
// return i; | ||
// } | ||
// } | ||
|
||
// }; |