← All Exercises

Go Loop and Range

basicgo~10min

Background

The range keyword in Go provides iteration over slices, maps, and strings with index and value access.

Problem

Write a Go function that takes a slice of strings and returns a map of string to its first occurrence index.

Input

A slice of strings, potentially with duplicates.

Output

A map[string]int mapping each unique string to its first index.

Sample tests

Sample 1

Input: ["a","b","a","c","b"]
Expected: {"a":0,"b":1,"c":3}

Your solution

Learning objective

Learn range iteration over slices, the comma-ok pattern for map existence, and index tracking.