CodeKata 저장용(3)

hmm...·2021년 6월 3일

CodeKata

목록 보기
3/4

문제

nums는 숫자로 이루어진 배열입니다.
가장 자주 등장한 숫자를 k 개수만큼 return 해주세요.

nums = [1,1,1,2,2,3],
k = 2

return [1,2]

nums = [1]
k = 1

return [1]

풀이

def top_k(nums, k):
    # 여기에 코드를 입력해주세요
  diction = {}
  lists = []
  final = []
  current = 0

  for i in nums:
    current = nums.count(i)
    diction[current] = i
  lists = list(diction.keys())
  lists.sort(reverse=True)

  for j in range(k):
    final.append(diction[lists[j]])
  
  return final
profile
위코드 21기

0개의 댓글