Esercitazione #4
Lezione 13 di 14 del corso Tabelle hash - Serie sulle strutture dati #4 di Coddy.
Sfida
FacileScrivi una funzione mostFrequent che riceva un array di interi non vuoto arr e restituisca l’elemento che compare più spesso.
Se due elementi hanno lo stesso conteggio massimo, restituisci quello che ha raggiunto quel conteggio per primo (nell’ordine di scansione).
Devi usare la classe HashMap fornita in hashmap.<ext> — non usare costrutti integrati nel linguaggio come set, dict o map.
Provalo tu
#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;
}
Tutte le lezioni di Tabelle hash - Serie sulle strutture dati #4
Esercitati da solo: Compilatore C online