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.5k Golang : Save image to PNG, JPEG or GIF format.
+11.6k Golang : Convert(cast) bigint to string
+10.2k Golang : Resolve domain name to IP4 and IP6 addresses.
+8.8k Golang : How to use Gorilla webtoolkit context package properly
+8.1k Golang : Count leading or ending zeros(any item of interest) example
+20.1k Nginx + FastCGI + Go Setup.
+28.9k Golang : missing Git command
+26.8k Golang : Find files by name - cross platform example
+11.1k Golang : Byte format example
+8.5k Golang : Get final balance from bit coin address example
+12.9k Golang : Convert(cast) int to int64
+13.8k Golang : Simple word wrap or line breaking example