Practice #4
Coddy의 Hash Table - 자료구조 시리즈 #4 코스 레슨 — 14개 중 13번째.
챌린지
쉬움비어 있지 않은 정수 배열 arr를 받아 가장 자주 나타나는 요소를 반환하는 mostFrequent 함수를 작성하세요.
두 요소의 빈도수가 동일한 경우, 해당 빈도수에 먼저 도달한 요소(스캔 순서상 더 앞선 요소)를 반환하세요.
반드시 hashmap.<ext>에 제공된 HashMap 클래스를 사용해야 합니다. 언어 내장 기능인 set, dict, map 등을 사용하지 마세요.
직접 해보기
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "solution.h"
int main() {
char line[8192];
if (!fgets(line, sizeof(line), stdin)) line[0] = '\0';
int arr[4096];
int n = 0;
char* tok = strtok(line, " \t\r\n");
while (tok) { arr[n++] = atoi(tok); tok = strtok(NULL, " \t\r\n"); }
int r = mostFrequent(arr, n);
printf("%d\n", r);
return 0;
}