Skip to content

Commit 9879785

Browse files
Jeehay28Jeehay28
authored andcommitted
Add missing-number solution in TS
1 parent b6d43ef commit 9879785

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

missing-number/Jeehay28.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
function missingNumber(nums: number[]): number {
4+
// 0, 1, 2, 3 => n * (n + 1) / 2
5+
6+
let sum = (nums.length * (nums.length + 1)) / 2;
7+
8+
for (const num of nums) {
9+
sum -= num;
10+
}
11+
12+
return sum;
13+
}
14+
15+
16+
// TC: O(n)
17+
// SC: (1)
18+
// function missingNumber(nums: number[]): number {
19+
// let xor = 0;
20+
21+
// for (let i = 0; i <= nums.length; i++) {
22+
// xor ^= i;
23+
// }
24+
25+
// for (const num of nums) {
26+
// xor ^= num;
27+
// }
28+
29+
// return xor;
30+
// }

0 commit comments

Comments
 (0)