-2

I have a Dictionary (Swift 5)

dict = ["ken" : 0, "Kendall" : 1, "kenny" : 2, "Sam" : 0, "Ben" : 3]

I'm trying to build a search function that returns the values for all keys/names containing the prefix

so if the input is "ken" it should return the values for the keys/names "ken", "Kendall", and "Kenny" because they all contain "ken" in their first 3 characters.

func search( string : String, dict : [String:Int] )->[Int] { }

returns [0,1,2]

Abraham
  • 19
  • 4

1 Answers1

0
let dict = ["ken" : 0, "Kendall" : 1, "kenny" : 2, "Sam" : 0, "Ben" : 3]

let result = dict.filter( { $0.key.lowercased().hasPrefix("ken") } ).values
Leonid Pavlov
  • 671
  • 8
  • 13