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

1 Answer

0 votes
let words = ["zero", "one", "two", "three", "four", "five", 
             "six", "seven", "eight", "nine", "ten"]
let specificLetter: Character = "t"

let indexes = words.enumerated().compactMap { index, word in
    word.lowercased().hasPrefix(String(specificLetter).lowercased()) ? index : nil
}

print(indexes) 


/*
run:

[2, 3, 10]

*/

 



answered Mar 14, 2025 by avibootz
...