Introduction

The Frequency Count problem involves counting how many times each element appears in an array or string.

Given:

  • an array
    or
  • a string

the task is to:

  • count frequency of every element
  • display occurrence count

This problem helps in understanding:

  • hashing
  • frequency counting
  • traversal techniques
  • data grouping

Example

Input:arr[] = [1,2,2,3,1,4,2]

Output:
1 → 2
2 → 3
3 → 1
4 → 1
Explanation: 1 appears 2 times
2 appears 3 times
3 appears 1 time
4 appears 1 time
Input:
s = "coding"

Output:
c → 1
o → 1
d → 1
i → 1
n → 1
g → 1
Explanation:
Every character appears once.

Constraints

 1 <= n <= 10^5 -10^9 <= arr[i] <= 10^9

Approach 1 : Brute Force

Explanation

The simplest way to solve this problem is:

  1. Traverse every element
  2. Count frequency manually
  3. Print frequencies

This approach is easy to understand but repeated counting increases time complexity.

Steps

  1. Traverse array elements.
  2. Count frequency of current element.
  3. Print frequency if not already processed.
  4. Continue traversal.

Dry Run

Input:arr = [1,2,2,3,1]

Check 1:
Frequency = 2
Print: 1 → 2

Check 2: Frequency = 2 Print: 2 → 2
Check 3:
Frequency = 1
Print:
3 → 1

Brute Force Code

Complexity Analysis

Time Complexity: O(n²)Explanation:
Nested traversal is used for counting frequencies.
Space Complexity: O(n) Explanation:
Visited array is used.

Approach 2 : Optimized Solution (Hashing)

Explanation

The optimized solution uses hashing.

The idea is:

  1. Store frequencies inside hashmap
  2. Traverse array once
  3. Print stored frequencies

This avoids repeated counting.

Steps

  1. Create hashmap.
  2. Traverse array.
  3. Increment frequencies.
  4. Print hashmap values.

Dry Run

Input:arr = [1,2,2,3,1]
Store frequencies: 1 → 2
2 → 2
3 → 1
Final Result: 1 → 2
2 → 2
3 → 1

Optimized Code

Complexity Analysis

Time Complexity: O(n)Explanation:
Each element is processed once.

Space Complexity: O(n)
Explanation:
Extra hashmap is used for frequencies.

Edge Cases

  1. Array contains duplicates
  2. Array contains all unique elements
  3. Array contains negative numbers
  4. Array contains one element
  5. Large input size

Why This Problem is Important

This problem helps in understanding:

  1. Hashing
  2. Frequency counting
  3. Efficient traversal
  4. Duplicate handling
  5. Data grouping

It is one of the most important hashing interview problems.

Real-World Applications

Frequency counting is used in:

  1. Data analytics
  2. Search engines
  3. Inventory systems
  4. Voting systems
  5. NLP processing

Common Mistakes

  1. Incorrect hashmap updates
  2. Forgetting duplicate handling
  3. Printing repeated frequencies
  4. Traversal mistakes

Interview Tips

Interviewers often expect:

  1. Hashing optimization
  2. Proper frequency logic
  3. O(n) solution explanation

Always explain why hashmap avoids repeated counting.

Related Questions

  1. Valid Anagram
  2. First Unique Character
  3. Majority Element
  4. Single Number
  5. Ransom Note

Final Takeaway

The Frequency Count problem is a fundamental hashing problem that teaches efficient counting and data grouping techniques. Understanding this problem builds a strong foundation for advanced hashing and frequency-based interview problems.