Esercitazione #1
Lezione 10 di 14 del corso Tabelle hash - Serie sulle strutture dati #4 di Coddy.
Sfida
FacileScrivi una funzione firstRepeated che riceve un array di interi arr e restituisce il primo elemento che compare più di una volta durante la scansione da sinistra a destra.
Se nessun elemento si ripete, restituisci -1.
Devi usare la classe HashMap fornita in hashmap.<ext> — non usare strutture integrate nel linguaggio come insiemi, dizionari o mappe.
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 = firstRepeated(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