Skip to content
Programming Problems · Q1

Q.Write a program to find the number of times an element occurs in the list.

CBSENCERTSubjective· 3mImportance★★★★★est
57% · 17/30 Questions
✓ Free question

Concept understanding — Element Frequency Count

Element Frequency Count — The Intuition

Imagine you have a bag of marbles. Some are red, some blue, some green. If someone asks "how many blue marbles are there?", you'd count them. That's exactly what element frequency count is — counting how many times each distinct item appears in a collection.

In programming terms, you have an array (or list) of elements, and you want to know: for each unique element, how many times does it occur?

Note

The "element" can be anything: a number, a character, a string, or even an object. The "frequency" is just the count of its occurrences.


The Precise Statement

Given a collection (array, list, string, etc.) of nn elements, the frequency count of an element xx is the number of times xx appears in that collection. Formally:

freq(x)=∑i=1n1(ai=x)\text{freq}(x) = \sum_{i=1}^{n} \mathbf{1}(a_i = x)

where 1(condition)\mathbf{1}(\text{condition}) is 1 when the condition is true and 0 otherwise.

The element frequency count of the entire collection is a mapping (or dictionary) from each distinct element to its frequency.


Why It Matters

This is one of the most fundamental operations in data analysis and algorithm design. You use it to:

  • Find the most/least frequent element (mode)
  • Detect duplicates
  • Build histograms
  • Solve pattern-matching problems
  • Analyse text (word frequencies)

How to Compute It — The Standard Method

The simplest approach uses a hash map (dictionary):

  1. Create an empty dictionary.
  2. Loop through each element in the collection.
  3. For each element:
    • If it's already in the dictionary, increment its count by 1.
    • Otherwise, add it to the dictionary with count 1.

In pseudocode:

freq = empty dictionary
for each element e in collection:
    if e in freq:
        freq[e] = freq[e] + 1
    else:
        freq[e] = 1
return freq
Tip

Many languages have a built-in shortcut. In Python, collections.Counter does exactly this in one line. In C++, std::unordered_map with ++freq[e] works because new keys are default-initialised to 0.


A Concrete Example

Take the array: [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

Step through:

ElementActionDictionary state
3new → count=1{3:1}
1new → count=1{3:1, 1:1}
4new → count=1{3:1, 1:1, 4:1}
1existing → increment{3:1, 1:2, 4:1}
5new → count=1{3:1, 1:2, 4:1, 5:1}
9new → count=1{3:1, 1:2, 4:1, 5:1, 9:1}
2new → count=1{3:1, 1:2, 4:1, 5:1, 9:1, 2:1}
6new → count=1{3:1, 1:2, 4:1, 5:1, 9:1, 2:1, 6:1}
5existing → increment{3:1, 1:2, 4:1, 5:2, 9:1, 2:1, 6:1}
3existing → increment{3:2, 1:2, 4:1, 5:2, 9:1, 2:1, 6:1}
5existing → increment{3:2, 1:2, 4:1, 5:3, 9:1, 2:1, 6:1}

Final result: 3 appears twice, 1 appears twice, 4 once, 5 three times, 9 once, 2 once, 6 once.


Complexity

  • Time: O(n)O(n) — you visit each element exactly once, and dictionary operations are O(1)O(1) on average.
  • Space: O(k)O(k) where kk is the number of distinct elements. In the worst case (all elements distinct), k=nk = n.
Watch out

If the elements are not hashable (e.g., lists in Python, or custom objects without a proper hash), you cannot use a hash map directly. You'd need a different approach, like sorting first (O(nlog⁡n)O(n \log n)) or using a balanced BST.


Common Variations

Frequency of a single element: Just count how many times that specific value appears — a simple loop with a counter.

Cumulative frequency: For sorted data, the frequency of an element plus all frequencies of elements before it.

Relative frequency: freq(x)n\frac{\text{freq}(x)}{n} — the proportion of the collection that is xx.

Frequency table: A table listing each distinct element alongside its frequency, often sorted by frequency or by element value.


The Key Insight

Element frequency count is just counting with memory. Instead of recounting from scratch each time you need a count, you build a lookup table once. That's the entire idea — and it's powerful enough to be the foundation of many algorithms you'll encounter later.

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.