Algorithmsfrequency count

Frequency Count (Counting Occurrences of Elements)

TT
Testlaa Team
May 14, 20261 min read

Frequency count is the atomic operation: given a multiset, how many of each key? It unlocks anagram checks, permutations with duplicates, and validity of rearrangements.

Why this shows up in the real world

Inventory systems count SKUs; text search builds term vectors; bioinformatics k-mer profiles are frequency vectors.

Core idea (explained for students)

collections.Counter(iterable) or manual dict with .get(k,0)+1. For equality of multisets compare two Counters with ==.

Try this in Python

from collections import Counter


def anagram(a: str, b: str) -> bool:
    return Counter(a) == Counter(b)


print(anagram('listen', 'silent'))

Common mistakes

  • Mutable values as dict keys (lists) — not allowed; use tuples.
  • Counting substrings vs subsequences—different structures.

Key takeaways

  • Counter supports + and - for multiset algebra.
  • For two strings anagram: Counter(a)==Counter(b).

Tags:

Hashing & frequencyPythonStudents