관리 메뉴

솜씨좋은장씨

[leetCode] 442. Find All Duplicates in an Array (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 442. Find All Duplicates in an Array (Python)

사용자 솜씨좋은장씨 2020. 10. 13. 22:30

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements that appear twice in this array.

Could you do it without extra space and in O(n) runtime?

 

Example:

Input:
[4,3,2,7,8,2,3,1]

Output:
[2,3]

Solution

from collections import Counter

class Solution:
    def findDuplicates(self, nums: List[int]) -> List[int]:
        cnt_items = Counter(nums).items()
        
        answer = [num[0] for num in cnt_items if num[1] > 1]
        
        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
댓글쓰기 폼