見出し画像

ほぼ日刊競プロ leetcode 136. Single Number

136. Single Number

Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.

考えたこと

pythonではcounterを使えるので,以下のように考えた.

import collections
class Solution:
   def singleNumber(self, nums: List[int]) -> int:
       temp =[k for k, v in collections.Counter(nums).items() if v == 1]
       return temp[0]

       import collections
class Solution:
   def singleNumber(self, nums: List[int]) -> int:
       c = collections.Counter(nums)
       return c.most_common()[-1][0]
       
       

いいなと思ったら応援しよう!