Skip to content

Commit 1283fe7

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 18 MB (8.51%)
1 parent ad083ec commit 1283fe7

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>You are given an integer array <code>nums</code>. The unique elements of an array are the elements that appear <strong>exactly once</strong> in the array.</p>
2+
3+
<p>Return <em>the <strong>sum</strong> of all the unique elements of </em><code>nums</code>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> nums = [1,2,3,2]
10+
<strong>Output:</strong> 4
11+
<strong>Explanation:</strong> The unique elements are [1,3], and the sum is 4.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [1,1,1,1,1]
18+
<strong>Output:</strong> 0
19+
<strong>Explanation:</strong> There are no unique elements, and the sum is 0.
20+
</pre>
21+
22+
<p><strong class="example">Example 3:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> nums = [1,2,3,4,5]
26+
<strong>Output:</strong> 15
27+
<strong>Explanation:</strong> The unique elements are [1,2,3,4,5], and the sum is 15.
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
35+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
36+
</ul>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
from collections import Counter
5+
6+
class Solution:
7+
def sumOfUnique(self, nums: List[int]) -> int:
8+
counter = Counter(nums)
9+
10+
total = 0
11+
for key, value in counter.items():
12+
if value == 1:
13+
total += key
14+
15+
return total
16+

0 commit comments

Comments
 (0)