Created
December 4, 2017 08:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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