Swift: How To Count Occurrences of a Character in a Swift String

Published under Mango Snippets, Jul 21, 2020

To count occurrences of a character in a Swift String, you can use Sequence.filter and then Array.count:

"Mango Snippets".filter({ $0 == "n"}).count

Output:

2