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
+26.6k Golang : Find files by name - cross platform example
+17.6k Golang : Convert IPv4 address to decimal number(base 10) or integer
+9.7k Golang : Edge detection with Sobel method
+9.8k Golang : Convert file unix timestamp to UTC time example
+30.8k Golang : How to convert(cast) string to IP address?
+23k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date
+5k Golang : If else example and common mistake
+7.5k Golang : Load DSA public key from file example
+11.3k Get form post value in Go
+12.2k Swift : Convert (cast) Int or int32 value to CGFloat
+13.9k Golang : Send email with attachment(RFC2822) using Gmail API example