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

1 Answer

0 votes
val words = Array("zero", "one", "two", "three", "four", "five", 
                  "six", "seven", "eight", "nine", "ten")
val specificLetter = 't'

val indexes = words.zipWithIndex.collect {
  case (word, index) if word.startsWith(specificLetter.toString) => index
}

println(indexes.mkString(", ")) 


 
/*
run:

2, 3, 10
  
*/

 



answered Mar 14, 2025 by avibootz
...