<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSCountedSet",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSCountedSet"
  },
  "title" : "NSCountedSet"
}
-->

# NSCountedSet

A mutable, unordered collection of distinct objects that may appear more than once in the collection.

```
class NSCountedSet
```

## Overview

Each distinct object inserted into an [`NSCountedSet`](/documentation/Foundation/NSCountedSet) object has a counter associated with it. [`NSCountedSet`](/documentation/Foundation/NSCountedSet) keeps track of the number of times objects are inserted and requires that objects be removed the same number of times. Thus, there is only one instance of an object in an [`NSSet`](/documentation/Foundation/NSSet) object even if the object has been added to the set multiple times. The [`count`](/documentation/Foundation/NSSet/count) method defined by the superclass [`NSSet`](/documentation/Foundation/NSSet) has special significance; it returns the number of distinct objects, not the total number of times objects are represented in the set. The [`NSSet`](/documentation/Foundation/NSSet) and [`NSMutableSet`](/documentation/Foundation/NSMutableSet) classes are provided for static and dynamic sets, respectively, whose elements are distinct.

While [`NSCountedSet`](/documentation/Foundation/NSCountedSet) and <doc://com.apple.documentation/documentation/CoreFoundation/CFBag> are not toll-free bridged, they provide similar functionality. For more information about `CFBag`, see the <doc://com.apple.documentation/documentation/CoreFoundation/CFBag>.

### Subclassing Notes

Because [`NSCountedSet`](/documentation/Foundation/NSCountedSet) is not a class cluster, it does not have primitive methods that provide the basis for its implementation. In general, there should be little need for subclassing.

#### Methods to Override

If you subclass [`NSCountedSet`](/documentation/Foundation/NSCountedSet), you must override any method of which you want to change the behavior.

If you change the primitive behavior of an [`NSCountedSet`](/documentation/Foundation/NSCountedSet), for instance if you change how objects are stored, you must override all of the affected methods. These include:

- [`add(_:)`](/documentation/Foundation/NSCountedSet/add(_:))
- [`remove(_:)`](/documentation/Foundation/NSCountedSet/remove(_:))
- [`objectEnumerator()`](/documentation/Foundation/NSCountedSet/objectEnumerator())
- [`count(for:)`](/documentation/Foundation/NSCountedSet/count(for:))

If you change the primitive behavior, you must also override the primitive methods of [`NSSet`](/documentation/Foundation/NSSet) and [`NSMutableSet`](/documentation/Foundation/NSMutableSet).

## Topics

### Initializing a Counted Set

[`init(array:)`](/documentation/Foundation/NSCountedSet/init(array:))

Returns a counted set object initialized with the contents of a given array.

[`init(set:)`](/documentation/Foundation/NSCountedSet/init(set:))

Returns a counted set object initialized with the contents of a given set.

[`init(capacity:)`](/documentation/Foundation/NSCountedSet/init(capacity:))

Returns a counted set object initialized with enough memory to hold a given number of objects.

### Adding and Removing Entries

[`add(_:)`](/documentation/Foundation/NSCountedSet/add(_:))

Adds a given object to the set.

[`remove(_:)`](/documentation/Foundation/NSCountedSet/remove(_:))

Removes a given object from the set.

### Combining and Recombining Sets

[unionSet:](/documentation/Foundation/nscountedset-unionset)

Adds each object in another given set to the receiving set, if not present.

[minusSet:](/documentation/Foundation/nscountedset-minusset)

Removes each object in another given set from the receiving set, if present.

[intersectSet:](/documentation/Foundation/nscountedset-intersectset)

Removes from the receiving set each object that isn’t a member of another given set.

### Examining a Counted Set

[`count(for:)`](/documentation/Foundation/NSCountedSet/count(for:))

Returns the count associated with a given object in the set.

[`objectEnumerator()`](/documentation/Foundation/NSCountedSet/objectEnumerator())

Returns an enumerator object that lets you access each object in the set once, independent of its count.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)