As @rmaddy already commented you can use Foundation NSCountedSet as follow:
import Foundation // or iOS UIKit or macOS Cocoa
let values = [65.0, 65.0, 65.0, 55.5, 55.5, 30.25, 30.25, 27.5]
let countedSet = NSCountedSet(array: values)
print(countedSet.count(for: 65.0)) // 3
for value in countedSet {
print("Element:", value, "count:", countedSet.count(for: value))
}
Xcode 11 • Swift 5.1
You can also extend NSCountedSet to return an array of tuples or a dictionary:
extension NSCountedSet {
var occurences: [(object: Any, count: Int)] { map { ($0, count(for: $0))} }
var dictionary: [AnyHashable: Int] {
reduce(into: [:]) {
guard let key = $1 as? AnyHashable else { return }
$0[key] = count(for: key)
}
}
}
let values = [65.0, 65.0, 65.0, 55.5, 55.5, 30.25, 30.25, 27.5]
let countedSet = NSCountedSet(array: values)
for (key, value) in countedSet.dictionary {
print("Element:", key, "count:", value)
}
For a Swift native solution we can extend Sequence
constraining its elements to Hashable
:
extension Sequence where Element: Hashable {
var frequency: [Element: Int] { reduce(into: [:]) { $0[$1, default: 0] += 1 } }
}
let values = [65.0, 65.0, 65.0, 55.5, 55.5, 30.25, 30.25, 27.5]
let frequency = values.frequency
frequency[65] // 3
for (key, value) in frequency {
print("Element:", key, "count:", value)
}
Those will print
Element: 27.5 count: 1
Element: 30.25 count: 2
Element: 55.5 count: 2
Element: 65 count: 3
If you have a collection of custom structure we can create a generic method and make use of keypath syntax as follow:
extension Sequence {
func sum<T: Hashable>(of property: (Element) -> T) -> [T: Int] {
reduce(into: [:]) { $0[property($1), default: 0] += 1 }
}
func sum<T: Hashable, A: AdditiveArithmetic>(of property: (Element) -> T, by adding: (Element) -> A) -> [T: A] {
reduce(into: [:]) { $0[property($1), default: .zero] += adding($1) }
}
}
Usage:
struct Product {
let id: String
let name: String
let quantity: Int
let price: Decimal
let total: Decimal
}
let products = [
("1", "iPhone", 2, Decimal(string: "499.99")!, Decimal(string: "999.98")!),
("2", "MacBook Pro", 1, Decimal(string: "2499.99")!, Decimal(string: "2499.99")!),
("3", "iPhone", 3, Decimal(string: "1199.99")!, Decimal(string: "3599.97")!),
("4", "iPhone", 1, Decimal(string: "999.99")!, Decimal(string: "999.99")!),
("5", "MacBook Pro", 2, Decimal(string: "1499.99")!, Decimal(string: "2999.98")!)
].map(Product.init)
let sum1 = products.sum(of: \.name)
sum1["iPhone"] // 3
sum1["MacBook Pro"] // 2
let sum2 = products.sum(of: \.name, by: \.quantity)
sum2["iPhone"] // 6
sum2["MacBook Pro"] // 3
let sum3 = products.sum(of: \.name, by: \.total)
sum3["iPhone"] // 5599.94
sum3["MacBook Pro"] // 5499.97