Found a cute way of counting the occurrences of character in a String:
let inputString = "test this string"
var frequencies : [Character: Int] = [:]
let baseCounts = zip(
inputString, repeatElement(1,count: Int.max))
frequencies = Dictionary(baseCounts, uniquingKeysWith: +)
with the result
["i": 2, "r": 1, "n": 1, "e": 1, "s": 3, " ": 2, "g": 1, "t": 4, "h": 1]
However I tried to use a range for the elements such that
let secondBaseCounts = zip(inputString, 0...)
frequencies = Dictionary(secondBaseCounts, uniquingKeysWith: +)
but get the incorrect result:
["i": 20, "r": 12, "n": 14, "e": 1, "s": 20, " ": 13, "g": 15, "t": 19, "h": 6]
Why?