- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSingleNumber.py
More file actions
Latest commit
42 lines (37 loc) · 929 Bytes
/
Copy pathSingleNumber.py
File metadata and controls
42 lines (37 loc) · 929 Bytes
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
'''
Given an array of integers,
every element appears twice except for one.
Find that single one.
Your algorithm should have a linear runtime complexity.
Could you implement it without using extra memory?
'''
'''
find the single occurence number in array of integers whose occurence are two
except one
args: array of integers
return: the once occurence number in the array
'''
defsingle_num(arr):
# create a map for the occurence of number
map= {}
fornuminarr:
ifnuminmap:
map[str(num)] +=1
else:
map[str(num)] =1
# look for the num
forkeyinmap:
ifmap[key] ==1:
returnkey
else:
returnNone
test= [1,2,2,4,4]
printsingle_num(test)
'''
Given an array of integers,
every element appears three times except for one.
Find that single one.
Note:
Your algorithm should have a linear runtime complexity.
Could you implement it without using extra memory?
'''