본문 바로가기

DataStructure/Array

Single Number

Given a non-empty 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?

Example 1:

Input: [2,2,1]
Output: 1

Example 2:

Input: [4,1,2,1,2]
Output: 4


My solution:

class Solution:
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
distinct = dict()
for num in nums:
distinct[num] = distinct.get(num, 0) + 1

for k, v in distinct.items():
if v == 1:
return k


'DataStructure > Array' 카테고리의 다른 글

Plus One  (0) 2018.10.29
Intersection of Two Arrays II  (0) 2018.10.29
Contains Duplicate  (0) 2018.10.29
Rotate Array  (0) 2018.10.29
Best Time to Buy and Sell Stock II  (0) 2018.10.29