We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 231f421 commit bd04f2fCopy full SHA for bd04f2f
1 file changed
single_Number.py
@@ -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