관리 메뉴

솜씨좋은장씨

[leetCode] 217. Contains Duplicate (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 217. Contains Duplicate (Python)

사용자 솜씨좋은장씨 2020. 10. 26. 01:53

Given an array of integers, find if the array contains any duplicates.

Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

 

Example 1:

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

Example 2:

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

Example 3:

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

Solution

from collections import Counter

class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        answer = False
        cnt = [c[1] for c in list(Counter(nums).items()) if c[1] > 1]
        
        if len(cnt) > 0:
            answer = True
            
        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

0 Comments
댓글쓰기 폼