Skip to content

Instantly share code, notes, and snippets.

@hernandazevedo
Created May 21, 2019 19:51
Show Gist options
  • Save hernandazevedo/7f94a09f394102052c65ce90e9feaaef to your computer and use it in GitHub Desktop.
Save hernandazevedo/7f94a09f394102052c65ce90e9feaaef to your computer and use it in GitHub Desktop.
func frequency(numbers: [Int]) -> [Int: Int] {
var frequencyDictionary: [Int:Int] = [:]
for number in numbers {
if let existentValue = frequencyDictionary[number] {
frequencyDictionary[number] = existentValue + 1
} else {
frequencyDictionary[number] = 1
}
}
return frequencyDictionary
}
print(frequency(numbers: [1, 3, 1, 1, 2, 7, 3, 5, 8, 5, 4, 9]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment