Skip to content

Commit bd04f2f

Browse files
committed
find single nums
1 parent 231f421 commit bd04f2f

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

single_Number.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Learn Python together
2+
3+
"""Given a non-empty array of integers nums,
4+
every element appears twice except for one.
5+
Find that single one."""
6+
7+
# Solution
8+
9+
def single_number(nums):
10+
# use dictionary comprehension to count nums
11+
count = {num: nums.count(num) for num in nums}
12+
# return minimum if nums
13+
return min(count, key=count.get)
14+
15+
#check
16+
17+
nums = [1, 2, 2, 3, 3]
18+
nums2 = [4, 3, 3, 2, 2]
19+
nums3 = [5]
20+
21+
print(single_number(nums)) # -> Output 1
22+
print(single_number(nums2)) # -> Output 4
23+
print(single_number(nums3)) # -> Output 5
24+

0 commit comments

Comments
 (0)