Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. """
  2. Given an array of integers, arr, where all numbers occur twice except one number
  3. which occurs once, find the number. Your solution should ideally be O(n) time
  4. and use constant extra space.
  5.  
  6. Example:
  7. >>> Solution().findSingle([7, 3, 5, 5, 4, 3, 4, 8, 8])
  8. 7
  9. """
  10.  
  11. class Solution(object):
  12. def findSingle(self, nums):
  13. s = set()
  14. for i in nums:
  15. if i in s:
  16. s.remove(i)
  17. else:
  18. s.add(i)
  19. return s.pop()
  20.  
  21. if __name__ == "__main__":
  22. import doctest
  23. doctest.testmod()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement