@@ -137,6 +137,19 @@ function singleNumber(nums: number[]): number {
137
137
}
138
138
```
139
139
140
+ #### JavaScript
141
+
142
+ ``` js
143
+ function singleNumber (nums ) {
144
+ let ans = 0 ;
145
+ for (let i = 0 ; i < 32 ; i++ ) {
146
+ const count = nums .reduce ((r , v ) => r + ((v >> i) & 1 ), 0 );
147
+ ans |= count % 3 << i;
148
+ }
149
+ return ans;
150
+ }
151
+ ```
152
+
140
153
#### Rust
141
154
142
155
``` rust
@@ -310,6 +323,22 @@ function singleNumber(nums: number[]): number {
310
323
}
311
324
```
312
325
326
+ #### JavaScript
327
+
328
+ ``` js
329
+ function singleNumber (nums ) {
330
+ let a = 0 ;
331
+ let b = 0 ;
332
+ for (const c of nums) {
333
+ const aa = (~ a & b & c) | (a & ~ b & ~ c);
334
+ const bb = ~ a & (b ^ c);
335
+ a = aa;
336
+ b = bb;
337
+ }
338
+ return b;
339
+ }
340
+ ```
341
+
313
342
#### Rust
314
343
315
344
``` rust
@@ -334,4 +363,74 @@ impl Solution {
334
363
335
364
<!-- solution: end -->
336
365
366
+ <!-- solution: start -->
367
+
368
+ ### Solution 3: Set + Math
369
+
370
+ <!-- tabs: start -->
371
+
372
+ #### TypeScript
373
+
374
+ ``` ts
375
+ function singleNumber(nums : number []): number {
376
+ const sumOfUnique = [... new Set (nums )].reduce ((a , b ) => a + b , 0 );
377
+ const sum = nums .reduce ((a , b ) => a + b , 0 );
378
+ return (sumOfUnique * 3 - sum ) / 2 ;
379
+ }
380
+ ```
381
+
382
+ #### JavaScript
383
+
384
+ ``` js
385
+ function singleNumber (nums ) {
386
+ const sumOfUnique = [... new Set (nums)].reduce ((a , b ) => a + b, 0 );
387
+ const sum = nums .reduce ((a , b ) => a + b, 0 );
388
+ return (sumOfUnique * 3 - sum) / 2 ;
389
+ }
390
+ ```
391
+
392
+ <!-- tabs: end -->
393
+
394
+ <!-- solution: end -->
395
+
396
+ <!-- solution: start -->
397
+
398
+ ### Solution 4: Bit Manipulation
399
+
400
+ <!-- tabs: start -->
401
+
402
+ #### TypeScript
403
+
404
+ ``` ts
405
+ function singleNumber(nums : number []): number {
406
+ let [ans, acc] = [0 , 0 ];
407
+
408
+ for (const x of nums ) {
409
+ ans ^= x & ~ acc ;
410
+ acc ^= x & ~ ans ;
411
+ }
412
+
413
+ return ans ;
414
+ }
415
+ ```
416
+
417
+ #### JavaScript
418
+
419
+ ``` ts
420
+ function singleNumber(nums ) {
421
+ let [ans, acc] = [0 , 0 ];
422
+
423
+ for (const x of nums ) {
424
+ ans ^= x & ~ acc ;
425
+ acc ^= x & ~ ans ;
426
+ }
427
+
428
+ return ans ;
429
+ }
430
+ ```
431
+
432
+ <!-- tabs: end -->
433
+
434
+ <!-- solution: end -->
435
+
337
436
<!-- problem: end -->
0 commit comments