관리 메뉴

솜씨좋은장씨

[leetCode] 136. Single Number (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 136. Single Number (Python)

솜씨좋은장씨 2020. 6. 13. 19:40
728x90
반응형

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

Solution

from collections import Counter

class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        cnt_dict = Counter(nums) 
        items = cnt_dict.items()
        
        for key, val in items:
            if val == 1:
                answer = key
                break
                
        return answer

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

 

Comments