Created
March 9, 2020 17:27
-
-
Save Maxatma/63e946b9ab0e6e74f0cf9853373a9456 to your computer and use it in GitHub Desktop.
Figure out appearance count of each element in array
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
extension Array where Element: Hashable { | |
var histogram: [Element: Int] { | |
return self.reduce(into: [:]) { counts, elem in counts[elem, default: 0] += 1 } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment