Skip to content

Commit 3abf25f

Browse files
committed
solve: missing number
1 parent cf99c48 commit 3abf25f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

β€Žmissing-number/tolluset.tsβ€Ž

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* TC: O(n)
3+
* SC: O(1)
4+
* */
5+
function missingNumber(nums: number[]): number {
6+
const n = nums.length;
7+
8+
const sum = (n * (n + 1)) / 2;
9+
const actualSum = nums.reduce((acc, curr) => acc + curr, 0);
10+
11+
if (sum === actualSum) {
12+
return 0;
13+
}
14+
15+
return sum - actualSum;
16+
}
17+
18+
const t1 = missingNumber([3, 0, 1]);
19+
console.info("πŸš€ : tolluset.ts:3: t1=", t1); // 2
20+
21+
const t2 = missingNumber([0, 1]);
22+
console.info("πŸš€ : tolluset.ts:6: t2=", t2); // 2
23+
24+
const t3 = missingNumber([9, 6, 4, 2, 3, 5, 7, 0, 1]);
25+
console.info("πŸš€ : tolluset.ts:9: t3=", t3); // 8

0 commit comments

Comments
Β (0)