-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_number.py
66 lines (44 loc) · 1.5 KB
/
single_number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# https://leetcode.com/problems/single-number/
# Given a non-empty array of integers nums, every element appears twice except for one.
# Find that single one.
# Follow up: Could you implement a solution with a linear runtime complexity and
# without using extra memory?
# Input: nums = [2,2,1]
# Output: 1
# Input: nums = [1]
# Output: 1
#==== SOLUTION 1 - Use dictionary ====
def singleNumber(nums):
if len(nums) == 1:
return nums[0]
# build a dictionary {array_element: count}
count_dict = {}
for i in nums:
if i not in count_dict:
count_dict[i] = 1
else:
count_dict[i] += 1
for key,value in count_dict.items():
if value == 1:
return key
#==== SOLUTION 2 - Math ====
def singleNumber(nums):
if len(nums) == 1:
return nums[0]
nums_set = set(nums) # create a set of from the array
nums_set_sum = sum(nums_set) # calculate the sum of the set
nums_arr_sum = sum(nums) # calculate the sum of the array
# the difference between 2x the sum of the set and the the sum of the array is the answer
ans = nums_set_sum*2 - nums_arr_sum
return ans
#==== SOLUTION 3 - Using set ====
def singleNumber(nums):
if len(nums) == 1:
return nums[0]
tracking_set = set()
for i in nums:
if i not in tracking_set:
tracking_set.add(i)
else:
tracking_set.remove(i)
return tracking_set.pop()