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
+9.9k Golang : Bcrypting password
+4.6k Fix Google Analytics Redundant Hostnames problem
+15.9k Golang : Loop each day of the current month example
+7.8k Findstr command the Grep equivalent for Windows
+18.4k Golang : Set, Get and List environment variables
+6.7k Get Facebook friends working in same company
+17.2k Golang : Linked list example
+11.5k How to tell if a binary(executable) file or web application is built with Golang?
+10k Golang : Embed secret text string into binary(executable) file
+32.8k Delete a directory in Go
+10.6k PHP : Convert(cast) bigInt to string
+21.9k Golang : Match strings by wildcard patterns with filepath.Match() function