Skip to content

Instantly share code, notes, and snippets.

@frakingFrank
Created December 4, 2017 08:45
Show Gist options
  • Save frakingFrank/197bd4381c410b09d59500f07590dd41 to your computer and use it in GitHub Desktop.
Save frakingFrank/197bd4381c410b09d59500f07590dd41 to your computer and use it in GitHub Desktop.
A Function that calculates which characters occur in a string, as well as how often each of these characters occur
func occurrencesOfCharacters(in text: String) -> [Character: Int] {
var dict: [Character: Int] = [:]
for key in text {
if dict[key] == nil {
dict[key] = 1
}else{
dict[key]! += 1
}
}
return dict
}
occurrencesOfCharacters(in: "abc bdccc")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment