Skip to content

Commit f430bf9

Browse files
authored
Create find-the-xor-of-numbers-which-appear-twice.py
1 parent a57113c commit f430bf9

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
# hash table
5+
class Solution(object):
6+
def duplicateNumbersXOR(self, nums):
7+
"""
8+
:type nums: List[int]
9+
:rtype: int
10+
"""
11+
return reduce(lambda x, y: x^y, nums, 0)^reduce(lambda x, y: x^y, set(nums), 0)
12+
13+
14+
# Time: O(n)
15+
# Space: O(n)
16+
import collections
17+
18+
19+
# freq table
20+
class Solution2(object):
21+
def duplicateNumbersXOR(self, nums):
22+
"""
23+
:type nums: List[int]
24+
:rtype: int
25+
"""
26+
return reduce(lambda x, y: x^y, (x for x, c in collections.Counter(nums).iteritems() if c == 2), 0)
27+

0 commit comments

Comments
 (0)