Skip to content

Commit 193148c

Browse files
committed
Unique Numbers
1 parent 76100ed commit 193148c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Facebook/Problem#424.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Given an array of integers in which two elements appear exactly once and all other
3+
elements appear exactly twice, find the two elements that appear only once.
4+
5+
For example, given the array [2, 4, 6, 8, 10, 2, 6, 10], return 4 and 8. The order does not matter.
6+
Follow-up: Can you do this in linear time and constant space?
7+
"""
8+
def singleNumber(nums: List[int]) -> List[int]:
9+
xor = 0
10+
for no in nums:
11+
xor ^= no
12+
res = [0, 0]
13+
xor &= -xor
14+
for no in nums:
15+
if xor & no == 0:
16+
res[0] ^= no
17+
else:
18+
res[1] ^= no
19+
return res

0 commit comments

Comments
 (0)