-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingle_Number.py
More file actions
163 lines (96 loc) · 3.74 KB
/
Single_Number.py
File metadata and controls
163 lines (96 loc) · 3.74 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'''
Edge Cases:
No element appears twice; it is a constraint so not possible
Single length array; return the only element already present in the array
len(nums) > 1; find the single element that does not appear twice
Approaches:
Brute Force
Intuition:
Iterate through every element in the nums and check if any of the element does not appear twice, in that case return the element.
Time: O(n^2)
Space: O(1)
Use Sorting
Intuition:
If the elements of the nums array are sorted/when we sort it, we can compare the neighbours to find the single element. It is already mentioned that all other elements appear twice except one.
Time: O(nlogn) for sorting then O(n) to check neighbouring elements
Space: O(1)
Use Hashing/Set
Intuition:
i) As we iterate through the nums array we store the elements encountered and check if we find them again while iteration continues. While checking if we find them again, we maintain a single_element object/variable which stores that single element, eventually returning the single_element.
ii) The other way is to maintain a num_frequency hashmap/dictionary and iterate over it to find which has exactly 1 frequency and return that key/num.
Time: O(n) for iterating over the nums array
Space: O(n) for hashing
Use Xor/Bit Manipulation
Intuition:
Xor of any two num gives the difference of bit as 1 and same bit as 0.
Thus, using this we get 1 ^ 1 == 0 because the same numbers have same bits.
So, we will always get the single element because all the same ones will evaluate to 0 and 0^single_number = single_number.
Time: O(n)
Space: O(1)
'''
# using Xor
class Solution:
def singleNumber(self, nums: List[int]) -> int:
xor = 0
for num in nums:
xor ^= num
return xor
# same XOR idea
'''
We use the nice property of XOR operation which is if you XOR same numbers it will return zero.
Since the nums contains just one non-repeating number,
we can just XOR all numbers together and the final result will be our answer.
'''
class Solution:
def singleNumber(self, nums: List[int]) -> int:
return reduce(lambda total, el: total ^ el, nums)
# another solution
class Solution:
def singleNumber(self, nums: List[int]) -> int:
## RC ##
## APPROACH : XOR ##
## TIME COMPLEXITY : O(N) ##
## SPACE COMPLEXITY : O(1) ##
# If we take XOR of zero and some bit, it will return that bit
# a XOR 0 = a, a XOR 0=a
# If we take XOR of two same bits, it will return 0
# a XOR a = 0 a XOR a=0
# a XOR b XOR a = (a XOR a) XOR b = 0 XOR b = b
# a⊕b⊕a=(a⊕a)⊕b=0⊕b=b
# So we can XOR all bits together to find the unique number.
a = 0
for i in nums:
a ^= i
return a
# minimised code
class Solution:
def singleNumber(self, nums):
dic = {}
for num in nums:
dic[num] = dic.get(num, 0)+1
for key, val in dic.items():
if val == 1:
return key
# another
class Solution:
def singleNumber(self, nums):
res = 0
for num in nums:
res ^= num
return res
# another
class Solution:
def singleNumber(self, nums):
return 2*sum(set(nums))-sum(nums)
# another
class Solution(object):
def singleNumber(self, nums):
return sum(list(set(nums)))*2 - sum(nums)
# another
class Solution:
def singleNumber(self, nums):
return reduce(lambda x, y: x ^ y, nums)
# another
class Solution:
def singleNumber(self, nums):
return reduce(operator.xor, nums)