Last active
August 3, 2020 14:59
-
-
Save xtrqt/b2716f5f69fe6e000ebdc3b567f534f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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