https://leetcode.com/problems/unique-number-of-occurrences
arr에 있는 숫자의 각 개수를 세는 데 반복문으로 일일히 세서 넣을 수도 있지만, collections를 사용하면 쉽게 개수를 셀 수 있다. collections 모듈의 Counter를 사용해, 해당 arr의 숫자와 개수를 dict 타입의 key와 value로 나타낼 수 있다.
이후 set을 사용해 숫자들의 개수가 겹치는 것이 있는 지 확인해 주었다.
import collections
class Solution:
def uniqueOccurrences(self, arr: List[int]) -> bool:
dictArr = collections.Counter(arr)
if len(dictArr) != len(set(dictArr.values())):
return False
else: return True
'Algorithm > Python' 카테고리의 다른 글
[백준] 16953 A ->B (0) | 2025.04.21 |
---|---|
[프로그래머스] 네트워크 (0) | 2024.12.24 |
[프로그래머스] 연속 부분 수열 합의 개수 (0) | 2023.05.31 |
[프로그래머스] 할인 행사 (0) | 2023.05.24 |
[프로그래머스] 전화번호 목록 (0) | 2023.05.24 |