Practice: Word Frequency
Split, group and sort.
Example
let text = "the cat and the hat and the bat"
var freq: [String: Int] = [:]
for w in text.split(separator: " ") { freq[String(w), default: 0] += 1 }
for (k, v) in freq.sorted(by: { $0.value != $1.value ? $0.value > $1.value : $0.key < $1.key }) { print(k, v) }Exercise
Default for missing key.
freq[w, : 0] += 1
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.