Skip to content

Commit a145ae1

Browse files
committed
[LeetCode Sync] Runtime - 3 ms (64.12%), Memory - 19.7 MB (42.18%)
1 parent af3fc4c commit a145ae1

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<p>Given a <strong>non-empty</strong>&nbsp;array of integers <code>nums</code>, every element appears <em>twice</em> except for one. Find that single one.</p>
2+
3+
<p>You must&nbsp;implement a solution with a linear runtime complexity and use&nbsp;only constant&nbsp;extra space.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<div class="example-block">
9+
<p><strong>Input:</strong> <span class="example-io">nums = [2,2,1]</span></p>
10+
11+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
12+
</div>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<div class="example-block">
17+
<p><strong>Input:</strong> <span class="example-io">nums = [4,1,2,1,2]</span></p>
18+
19+
<p><strong>Output:</strong> <span class="example-io">4</span></p>
20+
</div>
21+
22+
<p><strong class="example">Example 3:</strong></p>
23+
24+
<div class="example-block">
25+
<p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p>
26+
27+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
28+
</div>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>
35+
<li><code>-3 * 10<sup>4</sup> &lt;= nums[i] &lt;= 3 * 10<sup>4</sup></code></li>
36+
<li>Each element in the array appears twice except for one element which appears only once.</li>
37+
</ul>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def singleNumber(self, nums: List[int]) -> int:
3+
count = Counter(nums)
4+
return next(key for key, value in count.items() if value == 1)

0 commit comments

Comments
 (0)