How to get the indexes of words from an array of strings that start with a specific letter in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"strings"
)

func getIndexes(words []string, letter string) []int {
	var indexes []int
	
	for i, word := range words {
		if strings.HasPrefix(word, letter) {
			indexes = append(indexes, i)
		}
	}
	
	return indexes
}

func main() {
	words := []string{"zero", "one", "two", "three", "four", "five", 
                      "six", "seven", "eight", "nine", "ten"}
	specificLetter := "t"
	
	indexes := getIndexes(words, specificLetter)
	
	fmt.Println(indexes) 
}



/*
run:

[2 3 10]

*/

 



answered Mar 14, 2025 by avibootz
...