Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Algorithm
\nComplexity Analysis
\nTime complexity : . We iterate through , taking time. We search the whole list to find whether there is duplicate number, taking time. Because search is in the for
loop, so we have to multiply both time complexities which is .
Space complexity : . We need a list of size to contain elements in .
\nAlgorithm
\nWe use hash table to avoid the time required for searching the elements.
\npop
popitem
to get itComplexity Analysis
\nTime complexity : . Time complexity of for
loop is . Time complexity of hash table(dictionary in python) operation pop
is .
Space complexity : . The space required by is equal to the number of elements in .
\nConcept
\n\n\n
\n\nComplexity Analysis
\nTime complexity : . sum
will call next
to iterate through .\nWe can see it as sum(list(i, for i in nums))
which means the time complexity is because of the number of elements() in .
Space complexity : . set
needs space for the elements in nums
Concept
\nSo we can XOR all bits together to find the unique number.
\n\nComplexity Analysis
\nTime complexity : . We only iterate through , so the time complexity is the number of elements in .
\nSpace complexity : .
\nAnalysis written by: @Ambition_Wang
\n