Introduction

The Frequency Count problem involves counting how many times each element appears in the array.

Given an array arr[] of size n, the task is to calculate the frequency of every distinct element.

This is one of the most important beginner-level Hashing problems and helps in understanding HashMap, frequency tracking, and efficient counting techniques.

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: arr[] = [5, 5, 5, 5]
Output:
5 → 4
Explanation:
Element 5 appears 4 times.

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 its occurrences manually
  3. Avoid recounting already processed elements

This approach is easy to understand but repeated traversals increase time complexity.

Steps

  1. Create a visited array.
  2. Traverse the array.
  3. If element is already counted:
    • skip it
  4. Otherwise:
    • count occurrences using another loop
  5. Print element frequency.

Dry Run

Input Array: [1, 2, 2, 3, 1, 4, 2]Traverse 1:
Count occurrences of 1
Frequency = 2
Traverse 2:
Count occurrences of 2
Frequency = 3
Traverse next 2:
Already counted
Skip it
Traverse 3:
Frequency = 1
Traverse 4:
Frequency = 1
Final Frequencies:
1 → 2
2 → 3
3 → 1
4 → 1

Brute Force Code

Complexity Analysis

Time Complexity: O(n²)
Explanation:
Every element may be compared with remaining elements.
Space Complexity: O(n)
Explanation:
Visited array is used.

Approach 2 : Optimized Solution (HashMap)

Explanation

Instead of repeatedly counting occurrences, we can use a HashMap.

The idea is:

  1. Traverse the array
  2. Store:
    • element → frequency
  3. Update count whenever element appears again

HashMap provides fast insertion and lookup operations.

Steps

  1. Create an empty HashMap.
  2. Traverse the array.
  3. For every element:
    • if already exists:
      • increment frequency
    • otherwise:
      • store frequency as 1
  4. Print all frequencies.

Dry Run

Input Array: [1, 2, 2, 3, 1, 4, 2]Initially:
HashMap = {}
Traverse 1:
Store:
1 → 1
Traverse 2:
Store:
2 → 1
Traverse next 2:
Update:
2 → 2
Traverse 3:
Store:
3 → 1
Traverse next 1:
Update:
1 → 2
Traverse 4:
Store:
4 → 1
Traverse last 2:
Update:
2 → 3
Final Frequencies:
1 → 2
2 → 3
3 → 1
4 → 1

Optimized Code

Complexity Analysis

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

Space Complexity: O(n)
Explanation: HashMap stores frequencies of elements.

Edge Cases

  1. Array contains all unique elements
  2. Array contains all same elements
  3. Array contains negative numbers
  4. Array contains one element
  5. Array contains zeros

Why This Problem is Important

This problem helps in understanding:

  1. HashMap usage
  2. Frequency tracking
  3. Counting techniques
  4. Fast lookup operations
  5. Efficient traversal

It is one of the most important beginner-level hashing problems.

Real-World Applications

Frequency counting is used in:

  1. Word counting systems
  2. Search engines
  3. Analytics platforms
  4. Inventory systems
  5. Voting systems

Common Mistakes

  1. Incorrect frequency updates
  2. Forgetting initialization
  3. Using arrays for large values
  4. Overwriting frequencies incorrectly

Interview Tips

Interviewers often expect:

  1. HashMap optimization
  2. O(n) solution
  3. Efficient counting explanation

Always explain why HashMap is preferred for frequency tracking.

Related Questions

  1. Contains Duplicate
  2. Majority Element
  3. Top K Frequent Elements
  4. Anagram Problems
  5. First Unique Character

Final Takeaway

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