Skip to content

Commit d4daa7b

Browse files
committed
missing number solution
1 parent 00f2cd3 commit d4daa7b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

missing-number/hyer0705.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(1)
3+
function missingNumber(nums: number[]): number {
4+
const n = nums.length;
5+
6+
const expectedSum = (n * (n + 1)) / 2;
7+
const currentSum = nums.reduce((acc, curr) => acc + curr, 0);
8+
9+
return expectedSum - currentSum;
10+
}
11+
12+
// Time Complexity: O(n^2)
13+
// Space Complexity: O(1)
14+
function missingNumber(nums: number[]): number {
15+
const n = nums.length;
16+
17+
for (let i = 0; i <= n; i++) {
18+
if (!nums.includes(i)) return i;
19+
}
20+
21+
return -1;
22+
}

0 commit comments

Comments
 (0)