File tree Expand file tree Collapse file tree 3 files changed +45
-0
lines changed
solution/0100-0199/0137.Single Number II Expand file tree Collapse file tree 3 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -343,4 +343,24 @@ impl Solution {
343
343
344
344
<!-- solution: end -->
345
345
346
+ <!-- solution: start -->
347
+
348
+ ### Solution 1: Set + Math
349
+
350
+ <!-- tabs: start -->
351
+
352
+ #### TypeScript
353
+
354
+ ``` ts
355
+ function singleNumber(nums : number []): number {
356
+ const sumOfUnique = [... new Set (nums )].reduce ((a , b ) => a + b , 0 );
357
+ const sum = nums .reduce ((a , b ) => a + b , 0 );
358
+ return (sumOfUnique * 3 - sum ) / 2 ;
359
+ }
360
+ ```
361
+
362
+ <!-- tabs: end -->
363
+
364
+ <!-- solution: end -->
365
+
346
366
<!-- problem: end -->
Original file line number Diff line number Diff line change @@ -334,4 +334,24 @@ impl Solution {
334
334
335
335
<!-- solution: end -->
336
336
337
+ <!-- solution: start -->
338
+
339
+ ### Solution 1: Set + Math
340
+
341
+ <!-- tabs: start -->
342
+
343
+ #### TypeScript
344
+
345
+ ``` ts
346
+ function singleNumber(nums : number []): number {
347
+ const sumOfUnique = [... new Set (nums )].reduce ((a , b ) => a + b , 0 );
348
+ const sum = nums .reduce ((a , b ) => a + b , 0 );
349
+ return (sumOfUnique * 3 - sum ) / 2 ;
350
+ }
351
+ ```
352
+
353
+ <!-- tabs: end -->
354
+
355
+ <!-- solution: end -->
356
+
337
357
<!-- problem: end -->
Original file line number Diff line number Diff line change
1
+ function singleNumber ( nums : number [ ] ) : number {
2
+ const sumOfUnique = [ ...new Set ( nums ) ] . reduce ( ( a , b ) => a + b , 0 ) ;
3
+ const sum = nums . reduce ( ( a , b ) => a + b , 0 ) ;
4
+ return ( sumOfUnique * 3 - sum ) / 2 ;
5
+ }
You can’t perform that action at this time.
0 commit comments