Minimal implementation of a counted Set in Swift.
We start by creating a CountedSet
instance using a Hashable
element type and by adding and removing objects:
let words = CountedSet<String>()
words.insert("Hello")
words.insert("World")
words.insert("Hello")
words.remove("World")
We can ask the CountedSet
for the count of an element, which is also possible using subscripting:
let helloCount = words.count(for: "Hello") // 2
let worldCount = words.count(for: "World") // nil
let anotherHelloCount = words["Hello"] // 2
Access the most occurring element and its count, which will be returned as an optional tuple containing the element and its count:
if let (element, count) = words.mostFrequent() {
// Do something with element and count
}
It is also possible to set a specific count for an element directly (to avoid having to insert it multiple times for example).
This operation returns a Bool
specifying whether or not the count changed:
let changed = words.setCount(42, for: "Result")