# Multiset

``public struct Multiset<T: Hashable>``

A Multiset (sometimes called a bag) is a special kind of set in which members are allowed to appear more than once. It’s possible to convert a multiset to a set: `let set = Set(multiset)`

Conforms to `Sequence`, `ExpressibleByArrayLiteral` and `Hashable`.

• ``` init() ```

Constructs an empty multiset.

#### Declaration

Swift

``public init()``
• ``` init(_:) ```

Constructs a multiset from a sequence, such as an array.

#### Declaration

Swift

``public init<S: Sequence>(_ elements: S) where S.Iterator.Element == T``
• ``` count ```

Number of elements stored in the multiset, including multiple copies.

#### Declaration

Swift

``public fileprivate(set) var count = 0``
• ``` isEmpty ```

Returns `true` if and only if `count == 0`.

#### Declaration

Swift

``public var isEmpty: Bool``
• ``` distinctCount ```

Number of distinct elements stored in the multiset.

#### Declaration

Swift

``public var distinctCount: Int``
• ``` distinctElements ```

A sequence containing the multiset’s distinct elements.

#### Declaration

Swift

``public var distinctElements: AnySequence<T>``
• ``` contains(_:) ```

Returns `true` if the multiset contains the given element.

#### Declaration

Swift

``public func contains(_ element: T) -> Bool``
• ``` count(_:) ```

Returns the number of occurrences of an element in the multiset.

#### Declaration

Swift

``public func count(_ element: T) -> Int``
• ``` insert(_:) ```

Inserts a single occurrence of an element into the multiset.

#### Declaration

Swift

``public mutating func insert(_ element: T)``
• ``` insert(_:occurrences:) ```

Inserts a number of occurrences of an element into the multiset.

#### Declaration

Swift

``public mutating func insert(_ element: T, occurrences: Int)``
• ``` remove(_:) ```

Removes a single occurrence of an element from the multiset, if present.

#### Declaration

Swift

``public mutating func remove(_ element: T)``
• ``` remove(_:occurrences:) ```

Removes a number of occurrences of an element from the multiset. If the multiset contains fewer than this number of occurrences to begin with, all occurrences will be removed.

#### Declaration

Swift

``public mutating func remove(_ element: T, occurrences: Int)``
• ``` removeAllOf(_:) ```

Removes all occurrences of an element from the multiset, if present.

#### Declaration

Swift

``public mutating func removeAllOf(_ element: T)``
• ``` removeAll(keepingCapacity:) ```

Removes all the elements from the multiset, and by default clears the underlying storage buffer.

#### Declaration

Swift

``public mutating func removeAll(keepingCapacity keep: Bool = false)``
• ``` makeIterator() ```

Provides for-in loop functionality. Generates multiple occurrences per element.

#### Declaration

Swift

``public func makeIterator() -> AnyIterator<T>``

#### Return Value

A generator over the elements.

• ``` description ```

A string containing a suitable textual representation of the multiset.

#### Declaration

Swift

``public var description: String``
• ``` init(arrayLiteral:) ```

Constructs a multiset using an array literal. Unlike a set, multiple copies of an element are inserted.

#### Declaration

Swift

``public init(arrayLiteral elements: T...)``
• ``` hashValue ```

The hash value. `x == y` implies `x.hashValue == y.hashValue`

#### Declaration

Swift

``public var hashValue: Int``