-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle-number.py
44 lines (35 loc) · 1.08 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
class Solution:
def singleNumber(self, nums: List[int]) -> int:
# using hashset
hashset = set()
for i in nums:
if i in hashset: # second instance
#nums.remove(pointer)
# above ~ removes all the future instances of the element from the array
hashset.discard(i)
#print(nums)
else: #first instance
hashset.add(i)
#print(hashset)
#print(hashset)
# convert hashset to list and fetch
nlist = list(hashset)
return nlist[0]
'''
#uses bit manipulation
el = 0
for i in nums:
el = i ^ el #XOR operation
return el
'''
'''NOT WORKING
#iterative approach
nums.sort()
i = 0
while i < len(nums)-1:
if nums[i]!=nums[i+1]:
print(nums[i+1])
return nums[i]
i+=1
return nums[i]
'''