How to count occurrences of an element in a Swift array?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Counting how many times a value appears in a Swift array is a simple linear scan problem. The most direct solution is to filter the array or reduce it while comparing each element to the target value.
The best method depends on whether you are doing a one-off count or many repeated counts. For one count, a single pass is fine. For many counts, building a frequency table is usually more efficient.
Count With filter
For a readable one-off count, filter is the most common answer.
This is concise and idiomatic Swift.
It works for any array element type that supports equality comparison, which usually means the type conforms to Equatable.
Count With reduce
If you want to stay in one explicit pass without creating an intermediate filtered array, use reduce.
This can be a good fit when you are already reducing for another reason or want complete control over the accumulation logic.
Make It Reusable
You can package the pattern in a helper function.
This keeps the calling code clear when the pattern appears often.
When a Frequency Dictionary Is Better
If you need counts for many different values, repeatedly scanning the array is wasteful. Build a frequency dictionary once.
This is especially useful when you have many lookup queries after the initial pass.
What About Custom Types?
For custom objects, the same approach works if the type conforms to Equatable.
Without Equatable, Swift does not know how to compare instances for equality.
Complexity Matters
A single count through an array is O(n). That is completely normal because you may need to inspect every element.
If you repeat that many times for different targets, the total cost becomes larger. That is when the frequency-dictionary approach pays off, because you do the scan once and look up counts afterward.
Common Pitfalls
A common mistake is optimizing too early. For one target in one array, filter { ... }.count is clear and usually good enough.
Another mistake is building a full frequency dictionary when you only need one count once. That adds complexity without real benefit.
Developers also sometimes forget that custom types must define equality meaningfully before element counting can work cleanly.
Finally, if the element type is floating-point, think carefully about equality rules before relying on exact comparisons.
Summary
- For a one-off count,
array.filter { $0 == target }.countis the clearest solution. - '
reducegives you the same result with explicit accumulation logic.' - For many count queries, build a frequency dictionary once.
- Custom element types usually need
Equatableconformance. - The right choice depends more on query pattern than on syntax preference.
Related reading
- How to count the frequency of the elements in an unordered list?
- How to count the number of occurrences of an element in a List
- How to count the number of true elements in a NumPy bool array
- How to create a 2D tensor of Ones and Zeros like so
- How to create a circular ImageView in Android?
- How to Create a circular progressbar in Android which rotates on it?
- How to create a decision boundary graph for kNN models in the Caret package?
- How to create a delayed queue in RabbitMQ?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.