136. single number

Description

Difficulty: Easy

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?

给出一个整数数组,有一个元素只出现了一次,其他所有元素都出现了两次。找出这个元素。

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class (object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
rslt = {}
for i in nums:
try:
rslt[i] += 1
except KeyError:
rslt[i] = 1
for j in rslt:
if rslt[j] == 1:
return j