Menu
Coddy logo textTech

Practice #1

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

challenge icon

Challenge

Easy

Write a function firstRepeated that gets an integer array arr and returns the first element that appears more than once when you scan from left to right.

If no element repeats, return -1.

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 = firstRepeated(arr, n);
    printf("%d\n", r);
    return 0;
}

All lessons in Hash Tables - Data Structures Series #4