We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b6d43ef commit 9879785Copy full SHA for 9879785
missing-number/Jeehay28.ts
@@ -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
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