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
+35.2k Golang : Convert(cast) int64 to string
+11k Golang : How to parse plain email text and process email header?
+13.9k Golang : Find location by IP address and display with Google Map
+37.5k Golang : Convert to io.ReadSeeker type
+19.5k Golang : Clean up null characters from input data
+4.8k Golang : Struct field tags and what is their purpose?
+15.7k Golang : Find file size(disk usage) with filepath.Walk
+9.7k Golang : Sieve of Eratosthenes algorithm
+22.7k Golang : Find biggest/largest number in array
+10.6k Golang : Convert(cast) float to int
+6.3k Golang : Not able to grep log.Println() output
+12.5k Golang : Human readable time elapsed format such as 5 days ago