I’m beginner trying to implement this logic can anyone suggest the logic.
func findLetterOccurence(Letter: String){
let array = ["Data", "program", "questions", "Helpful"]
///Logic to find the given letter occurences in string array
print("(Letter) occured in (count) times")
}
Expected Output: a occured in 3 times
I’ve tried as below:
var count = 0
for i in array {
var newArray.append(i)
count = components(separatedBy: newArray).count - 1
}
But I don’t understand what exactly the logic inside components(separatedBy:) ? I mean without higher function how can we implment logic here.
3
Answers
try something like this:
You have to adjust if you want case insensitive, like this:
add this extension to find occurance of latter in string
use it for array
A couple of ways.
You could also do a sensitive compare and leave the casing alone
by saying
Another way would be this