Menu
Coddy logo textTech

Practice #4

Lesson 13 of 14 in Coddy's Hash Tables - Data Structures Series #4 course.

challenge icon

Challenge

Easy

Write a function mostFrequent that gets a non-empty integer array arr and returns the element that appears the most often.

If two elements tie for the highest count, return the one that reached that count first (earliest in scan order).

You must use the HashMap class provided in hashmap.<ext> — do not use language built-ins like sets, dicts, or maps.

Try it yourself

#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;
}

All lessons in Hash Tables - Data Structures Series #4