Single Number
Question
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Example 1: Input: [2,2,1] Output: 1
Example 2: Input: [4,1,2,1,2] Output: 4
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Approach 1: Math
Concept
1 | class Solution: |
- Runtime: 32 ms, faster than 99.87% of Python3 online submissions for Single Number.
- Memory Usage: 15.1 MB, less than 14.91% of Python3 online submissions for Single Number.
Approach 2: Hash Table
1 | class Solution: |
- Runtime: 44 ms, faster than 68.21% of Python3 online submissions for Single Number.
- Memory Usage: 15.1 MB, less than 12.23% of Python3 online submissions for Single Number.