Skip to content

Instantly share code, notes, and snippets.

@GlulkAlex
Created June 9, 2016 15:37
Show Gist options
  • Save GlulkAlex/a1961c8d278a790f4639522ece0c1d48 to your computer and use it in GitHub Desktop.
Save GlulkAlex/a1961c8d278a790f4639522ece0c1d48 to your computer and use it in GitHub Desktop.
filter sequence of odd integer elements using bitwise exclusive or
def lonely_Integer(
b: list# of int
) -> int:
so_Far = set()
answer = 0
for n in b:
if n in so_Far:
so_Far.discard(n)
else:
so_Far.add(n)
if len(so_Far) > 0:
answer = so_Far.pop()
return answer
def lonelyinteger(
b: list# of int
) -> int:
"""
>>> 4 ^ 9 ^ 95 ^ 93 ^ 57 ^ 4 ^ 57 ^ 93 ^ 9
95
"""
answer = -1#None#0
#assert len(list(b)) % 2 != 0# odd
for n in b:
assert 0 <= n <= 100
if answer == -1:
answer = n
else:
answer = answer ^ n
return answer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment