[{"data":1,"prerenderedAt":435},["ShallowReactive",2],{"post-document:\u002Falgorithm\u002F2026\u002F08\u002F14\u002Fcounter\u002F":3},{"id":4,"title":5,"body":6,"categories":419,"date":421,"description":422,"extension":423,"image":424,"key_concepts":424,"last_modified_at":424,"legacyPath":425,"meta":426,"navigation":76,"part":424,"path":428,"published":76,"robots":424,"seo":429,"series":424,"stem":430,"strengths":424,"summary":424,"tags":431,"tradeoffs":424,"__hash__":434},"posts\u002Fposts\u002FAlgorithm\u002FPython 코테\u002F2026-08-14-counter.md","Python Counter, 빈도수를 쉽게 세는 방법",{"type":7,"value":8,"toc":407},"minimark",[9,14,21,31,47,50,62,101,109,112,136,139,143,182,185,214,219,241,244,253,255,259,262,265,268,271,282,285,288,292,316,320,328,335,341,343,347,367,370,390,392,395,403],[10,11,13],"h2",{"id":12},"counter","Counter",[15,16,17],"blockquote",{},[18,19,20],"p",{},"빈도수를 세는 문제에서 코드를 확 줄여주는 기능이다.",[18,22,23,26,27,30],{},[24,25,13],"code",{},"는 ",[24,28,29],{},"collections"," 모듈에서 가져와 사용한다.",[32,33,38],"pre",{"className":34,"code":35,"language":36,"meta":37,"style":37},"language-python shiki shiki-themes github-light github-dark","from collections import Counter\n","python","",[24,39,40],{"__ignoreMap":37},[41,42,45],"span",{"class":43,"line":44},"line",1,[41,46,35],{},[18,48,49],{},"기본 형태는 다음과 같다.",[15,51,52],{},[18,53,54,57,58,61],{},[24,55,56],{},"Counter(이터러블)"," → ",[24,59,60],{},"{원소: 등장 횟수}"," 형태로 세어준다.",[32,63,65],{"className":34,"code":64,"language":36,"meta":37,"style":37},"from collections import Counter\n\nnums = [1, 2, 2, 3, 3, 3]\ncount = Counter(nums)\n\nprint(count[2])\n",[24,66,67,71,78,84,90,95],{"__ignoreMap":37},[41,68,69],{"class":43,"line":44},[41,70,35],{},[41,72,74],{"class":43,"line":73},2,[41,75,77],{"emptyLinePlaceholder":76},true,"\n",[41,79,81],{"class":43,"line":80},3,[41,82,83],{},"nums = [1, 2, 2, 3, 3, 3]\n",[41,85,87],{"class":43,"line":86},4,[41,88,89],{},"count = Counter(nums)\n",[41,91,93],{"class":43,"line":92},5,[41,94,77],{"emptyLinePlaceholder":76},[41,96,98],{"class":43,"line":97},6,[41,99,100],{},"print(count[2])\n",[32,102,107],{"className":103,"code":105,"language":106,"meta":37},[104],"language-text","2\n","text",[24,108,105],{"__ignoreMap":37},[18,110,111],{},"문자열도 문자 하나씩 순회할 수 있는 이터러블이므로 카운팅할 수 있다.",[32,113,115],{"className":34,"code":114,"language":36,"meta":37,"style":37},"count = Counter(\"banana\")\n\nprint(count)\n# Counter({'a': 3, 'n': 2, 'b': 1})\n",[24,116,117,122,126,131],{"__ignoreMap":37},[41,118,119],{"class":43,"line":44},[41,120,121],{},"count = Counter(\"banana\")\n",[41,123,124],{"class":43,"line":73},[41,125,77],{"emptyLinePlaceholder":76},[41,127,128],{"class":43,"line":80},[41,129,130],{},"print(count)\n",[41,132,133],{"class":43,"line":86},[41,134,135],{},"# Counter({'a': 3, 'n': 2, 'b': 1})\n",[137,138],"hr",{},[10,140,142],{"id":141},"알고리즘-문제에서-자주-쓰는-기능","알고리즘 문제에서 자주 쓰는 기능",[32,144,146],{"className":34,"code":145,"language":36,"meta":37,"style":37},"count = Counter(nums)\n\ncount[x]              # x의 개수, 없으면 0\ncount.most_common(1)  # 가장 많이 나온 값 1개\ncount.keys()          # 등장한 값들\ncount.values()        # 각 값의 등장 횟수\ncount.items()         # (값, 등장 횟수)\n",[24,147,148,152,156,161,166,171,176],{"__ignoreMap":37},[41,149,150],{"class":43,"line":44},[41,151,89],{},[41,153,154],{"class":43,"line":73},[41,155,77],{"emptyLinePlaceholder":76},[41,157,158],{"class":43,"line":80},[41,159,160],{},"count[x]              # x의 개수, 없으면 0\n",[41,162,163],{"class":43,"line":86},[41,164,165],{},"count.most_common(1)  # 가장 많이 나온 값 1개\n",[41,167,168],{"class":43,"line":92},[41,169,170],{},"count.keys()          # 등장한 값들\n",[41,172,173],{"class":43,"line":97},[41,174,175],{},"count.values()        # 각 값의 등장 횟수\n",[41,177,179],{"class":43,"line":178},7,[41,180,181],{},"count.items()         # (값, 등장 횟수)\n",[18,183,184],{},"예를 들어 각 귤 크기의 개수를 센 뒤, 등장 횟수만 가져오고 싶다면 다음처럼 사용할 수 있다.",[32,186,188],{"className":34,"code":187,"language":36,"meta":37,"style":37},"tangerine = [1, 3, 2, 5, 4, 5, 2, 3]\ncounts = list(Counter(tangerine).values())\n\nprint(counts)\n# [1, 2, 2, 2, 1]\n",[24,189,190,195,200,204,209],{"__ignoreMap":37},[41,191,192],{"class":43,"line":44},[41,193,194],{},"tangerine = [1, 3, 2, 5, 4, 5, 2, 3]\n",[41,196,197],{"class":43,"line":73},[41,198,199],{},"counts = list(Counter(tangerine).values())\n",[41,201,202],{"class":43,"line":80},[41,203,77],{"emptyLinePlaceholder":76},[41,205,206],{"class":43,"line":86},[41,207,208],{},"print(counts)\n",[41,210,211],{"class":43,"line":92},[41,212,213],{},"# [1, 2, 2, 2, 1]\n",[215,216,218],"h3",{"id":217},"특히-이런-문제에서-유용하다","특히 이런 문제에서 유용하다",[220,221,222,226,229,232,235,238],"ul",{},[223,224,225],"li",{},"문자열의 문자 개수 세기",[223,227,228],{},"숫자별 등장 횟수 세기",[223,230,231],{},"애너그램 판별",[223,233,234],{},"최빈값 찾기",[223,236,237],{},"두 배열의 원소 구성 비교",[223,239,240],{},"중복 원소 찾기",[18,242,243],{},"애너그램은 문자의 순서는 다르지만, 사용된 문자와 각 문자의 개수가 같은 단어를 말한다.",[32,245,247],{"className":34,"code":246,"language":36,"meta":37,"style":37},"Counter(\"listen\") == Counter(\"silent\")  # True\n",[24,248,249],{"__ignoreMap":37},[41,250,251],{"class":43,"line":44},[41,252,246],{},[137,254],{},[10,256,258],{"id":257},"개인적인-고민","개인적인 고민",[18,260,261],{},"모든 기능을 내가 구현해야 하는 게 아닌가?",[18,263,264],{},"나는 약간 내가 직접 구현해야 할 것 같은 강박이 있다.",[18,266,267],{},"하지만 이런 강박을 가질 필요는 없다.",[18,269,270],{},"알고리즘 문제에서는 단순히 코드를 길게 짜는 능력만 보는 것이 아니다.",[220,272,273,276,279],{},[223,274,275],{},"이 문제가 빈도수를 활용해서 해결할 수 있는 문제인지 알아보는가?",[223,277,278],{},"어떤 자료구조를 사용해야 하는지 판단할 수 있는가?",[223,280,281],{},"시간 복잡도를 고려할 수 있는가?",[18,283,284],{},"빈도수를 딕셔너리에 저장하는 반복문은 이미 검증된 도구로 대체할 수 있는 구현 세부 사항이다.",[18,286,287],{},"굳이 검증된 도구가 있다면 매번 다시 만들어서 쓸 필요는 없다.",[215,289,291],{"id":290},"직접-구현한다면","직접 구현한다면",[32,293,295],{"className":34,"code":294,"language":36,"meta":37,"style":37},"count = {}\n\nfor x in nums:\n    count[x] = count.get(x, 0) + 1\n",[24,296,297,302,306,311],{"__ignoreMap":37},[41,298,299],{"class":43,"line":44},[41,300,301],{},"count = {}\n",[41,303,304],{"class":43,"line":73},[41,305,77],{"emptyLinePlaceholder":76},[41,307,308],{"class":43,"line":80},[41,309,310],{},"for x in nums:\n",[41,312,313],{"class":43,"line":86},[41,314,315],{},"    count[x] = count.get(x, 0) + 1\n",[215,317,319],{"id":318},"counter를-사용한다면","Counter를 사용한다면",[32,321,322],{"className":34,"code":89,"language":36,"meta":37,"style":37},[24,323,324],{"__ignoreMap":37},[41,325,326],{"class":43,"line":44},[41,327,89],{},[18,329,330,331,334],{},"둘 다 핵심 원리는 똑같고, 평균 시간 복잡도도 보통 ",[24,332,333],{},"O(N)","이다.",[18,336,337,338,340],{},"따라서 ",[24,339,13],{},"를 사용했다고 해서 알고리즘을 건너뛴 것은 아니다.",[137,342],{},[10,344,346],{"id":345},"추천-학습-방식","추천 학습 방식",[348,349,350,353,359,362],"ol",{},[223,351,352],{},"처음에는 빈도수 계산을 직접 구현해보며 원리를 이해한다.",[223,354,355,356,358],{},"이후 문제에서는 ",[24,357,13],{},"를 사용한다.",[223,360,361],{},"라이브러리 사용이 금지된 문제나 코딩 테스트 환경에서 지원하지 않는 경우에만 직접 구현한다.",[223,363,364,366],{},[24,365,13],{}," 없이도 구현 방법을 설명할 수 있으면 충분하다.",[18,368,369],{},"다만 아래 알고리즘과 자료구조는 동작 원리를 이해하기 위해 직접 구현해보는 것이 좋다.",[220,371,372,375,378,381,384,387],{},[223,373,374],{},"정렬, 이분 탐색",[223,376,377],{},"스택, 큐",[223,379,380],{},"BFS, DFS",[223,382,383],{},"힙",[223,385,386],{},"유니온 파인드",[223,388,389],{},"다익스트라",[137,391],{},[10,393,394],{"id":394},"결론",[15,396,397,400],{},[18,398,399],{},"문제가 빈도수 문제라는 것을 알아보는 것도 알고리즘 실력이다.",[18,401,402],{},"원리는 직접 이해하고, 실전에서는 표준 라이브러리를 적극적으로 사용하자.",[404,405,406],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":37,"searchDepth":73,"depth":73,"links":408},[409,410,413,417,418],{"id":12,"depth":73,"text":13},{"id":141,"depth":73,"text":142,"children":411},[412],{"id":217,"depth":80,"text":218},{"id":257,"depth":73,"text":258,"children":414},[415,416],{"id":290,"depth":80,"text":291},{"id":318,"depth":80,"text":319},{"id":345,"depth":73,"text":346},{"id":394,"depth":73,"text":394},[420],"Algorithm","2026-08-14 09:00:00 +0900","Python collections.Counter로 원소의 빈도수를 세고 알고리즘 문제에 활용하는 방법을 정리한다.","md",null,"\u002Falgorithm\u002F2026\u002F08\u002F14\u002Fcounter\u002F",{"layout":427},"post","\u002Fposts\u002Falgorithm\u002Fpython\u002F2026-08-14-counter",{"title":5,"description":422},"posts\u002FAlgorithm\u002FPython 코테\u002F2026-08-14-counter",[432,420,13,433],"Python","Collections","92PlfdiFwgI2MrOlsEYF1IqBgfafVxYIY4LrRRTcIvU",1788744784072]