Golang : How to count the number of repeated characters in a string?
Problem :
You want to develop a new algorithm that somehow will reduce or compress the size of a string significantly. In order to do that you need to determine how many characters are repeated in a string. How to count the number of repeated characters?
Solution :
Use the strings.Count()
function to find out the number of repeated characters.
For example :
package main
import (
"fmt"
"strings"
)
func main() {
str := "a long string with many repeated characters"
numberOfa := strings.Count(str, "a")
fmt.Printf("[%v] string has %d of characters of [a] ", str, numberOfa)
}
Output :
[a long string with many repeated characters] string has 5 of characters of [a]
Reference :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+14.8k Golang : Find commonalities in two slices or arrays example
+11.6k Get form post value in Go
+9.7k PHP : Get coordinates latitude/longitude from string
+33.8k Golang : convert(cast) bytes to string
+10.5k Golang : Select region of interest with mouse click and crop from image
+22.9k Golang : Test file read write permission example
+10.5k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+21.7k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+5.2k Golang : The Tao of importing package
+7k Golang : Gargish-English language translator
+11.3k Golang : Characters limiter example
+10.1k Golang : Get login name from environment and prompt for password