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
+4.4k Golang : Converting individual Jawi alphabet to Rumi(Romanized) alphabet example
+10.8k Golang : Flip coin example
+6.1k Facebook : How to force facebook to scrape latest URL link data?
+20.8k Nginx + FastCGI + Go Setup.
+8.1k Javascript : How to check a browser's Do Not Track status?
+5.5k Gogland : Datasource explorer
+11.7k Swift : Convert (cast) Float to String
+14.7k Golang : How to pass map to html template and access the map's elements
+9.9k Golang : Detect number of active displays and the display's resolution
+30.3k Golang : Get time.Duration in year, month, week or day
+14.2k Golang : Google Drive API upload and rename example
+7.5k Golang : Calculate how many weeks left to go in a given year