Home > Software design >  How to find occurences of a lettter in the string array in swift
How to find occurences of a lettter in the string array in swift

Time:09-16

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.

CodePudding user response:

try something like this:

 func findLetterOccurence(letter: String) {
    var count = 0
    for word in array { count  = word.filter{ String($0) == letter}.count }
    print("--> \(letter) occured in \(count) times")
}

You have to adjust if you want case insensitive, like this:

func findLetterOccurence(letter: String) {
    var count = 0
    for word in array { count  = word.filter{ String($0).lowercased() == letter.lowercased()}.count }
    print("--> \(letter) occured in \(count) times")
}

CodePudding user response:

A couple of ways.

    @discardableResult func findLetterOccurence(letter: String) -> Int {
    
    let array = ["Data", "program", "questions", "Helpful"]
    var count = 0
    // here we join the array into a single string. Then for each character we check if the lowercased version matches the string lowercased value. 
    array.joined().forEach({ if $0.lowercased() == letter.lowercased() { count  = 1} } )
    
    print("\(letter) occured in \(count) times")
    
    return count
}

You could also do a sensitive compare and leave the casing alone by saying

array.joined().forEach({ if String($0) == letter { count  = 1} } )

Another way would be this

//here our argument is a character because maybe we just want to search for a single letter.
    @discardableResult func findLetterOccurence2(character: Character) -> Int {
    
        let array = ["Data", "program", "questions", "Helpful"]
   
    //again join the array into a single string. and reduce takes the `into` parameter and passes it into the closure as $0 in this case, and each element of the string gets passed in the second argument of the closure.
        let count = array.joined().reduce(into: 0) {
           $0  = $1.lowercased() == letter.lowercased() ? 1 : 0
        }
        print("\(letter) occured in \(count) times")
    
        return count
    }

CodePudding user response:

add this extension to find occurance of latter in string

extension String {
    func numberOfOccurrencesOf(string: String) -> Int {
        return self.components(separatedBy:string).count - 1
    }
}

use it for array

        func findLetterOccurence(Letter: String){
        let array = ["Data", "program", "questions", "Helpful"]
        var number = 0
        for str in array{
            var l = Letter
//            uncomment it to use code for upper case and lower case both
//            l = str.lowercased()
            
            number = number   str.numberOfOccurrencesOf(string: l)
        }
        print("\(Letter) occured in \(number) times")
    }
  • Related