Skip to content

Instantly share code, notes, and snippets.

@xtrqt
Last active August 3, 2020 14:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtrqt/b2716f5f69fe6e000ebdc3b567f534f4 to your computer and use it in GitHub Desktop.
Save xtrqt/b2716f5f69fe6e000ebdc3b567f534f4 to your computer and use it in GitHub Desktop.
"""
Given an array of integers, arr, where all numbers occur twice except one number
which occurs once, find the number. Your solution should ideally be O(n) time
and use constant extra space.
Example:
>>> Solution().findSingle([7, 3, 5, 5, 4, 3, 4, 8, 8])
7
"""
class Solution(object):
def findSingle(self, nums):
s = set()
for i in nums:
if i in s:
s.remove(i)
else:
s.add(i)
return s.pop()
if __name__ == "__main__":
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment