Skip to content

Instantly share code, notes, and snippets.

@st0le
Last active December 17, 2015 19:29
Show Gist options
  • Save st0le/5660547 to your computer and use it in GitHub Desktop.
Save st0le/5660547 to your computer and use it in GitHub Desktop.
2 Missing Duplicate
import random
r = {random.randint(10,100) for i in xrange(10)}
lst = random.sample(r,len(r))
l = lst[:2] + lst[2:] * 2
random.shuffle(l)
def missing_2_numbers(lst):
xor = reduce(lambda a,b : a ^ b, lst)
mask = (xor & -xor) #get the right most set bit.
xor_1 = 0
for v in lst:
if v & mask:
xor_1 = xor_1 ^ v
return (xor_1,xor ^ xor_1)
print l
print missing_2_numbers(l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment