How to find String repetition count?

Hi Team,
I need to find String repetition count. For Example
I am returning 5 String Values as below
"Success"
“Success”
“Success”
“Failed”
“Failed”

I need to find a count for Success and failed .

Success=3 times
Failed=2 times

Thanks,
Rajapandian.B

What have you tried so far? What result do you get with your code so far and what result do you expect instead?

Preferably post some sscce on the playground.

Hi, have you tried to use a map to perform this count?
Try this:

func main() {
    counter := make(map[string]int)
    values := [5]string{"Success", "Success", "Success", "Failure", "Failure"}

    for _, element := range values {
	    counter[element] += 1
    }

    fmt.Println(counter["Success"])  // 3
    fmt.Println(counter["Failure"])  // 2
}

Thanks Britz

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.