Frequency Counting

Frequency counting refers to tallying the number of occurrences of each value in a dataset. This allows analyzing the distribution of values.

Java example:

1
2
3
4
5
6
7
8
9
Map<Integer, Integer> countFreq(int[] arr) {
  Map<Integer, Integer> freq = new HashMap<>();

  for (int n : arr) {
    freq.put(n, freq.getOrDefault(n, 0) + 1);
  }

  return freq;
}

C++ example:

1
2
3
4
5
6
7
8
9
map<int, int> countFreq(vector<int> arr) {
  map<int, int> freq; 

  for (int n : arr) {
    freq[n]++;
  }

  return freq;
}

Python example:

1
2
3
4
5
6
7
8
def count_freq(arr):

  freq = {}

  for n in arr:
    freq[n] = freq.get(n, 0) + 1

  return freq

The general approach is:

  • Initialize a frequency map
  • Iterate through array, incrementing map counts
  • Return final map

Frequency counting provides insights into data distribution and is useful for statistics, data analysis, and machine learning.

Frequency Counting is a commonly used technique to track the occurrence of elements within a collection, such as an array or a list. It’s often used in algorithms to analyze the distribution of elements and helps in solving problems related to counting, such as finding the most frequent element, determining if two strings are anagrams, etc.

The core idea is to use a data structure like a hash table, where keys are the unique elements from the collection, and the values are the counts of occurrences of those elements. By iterating through the collection and incrementing the count for each element in the hash table, we can efficiently obtain the frequency of each element.

Solution

Java Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import java.util.HashMap;

public void frequencyCount(int[] arr) {
    HashMap<Integer, Integer> frequencyMap = new HashMap<>();

    for (int num : arr) {
        frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
    }

    // Print the frequencies
    for (Integer key : frequencyMap.keySet()) {
        System.out.println("Number " + key + " occurs " + frequencyMap.get(key) + " times");
    }
}

C++ Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <iostream>
#include <unordered_map>

void frequencyCount(int arr[], int n) {
    std::unordered_map<int, int> frequencyMap;

    for (int i = 0; i < n; ++i) {
        frequencyMap[arr[i]]++;
    }

    // Print the frequencies
    for (auto& pair : frequencyMap) {
        std::cout << "Number " << pair.first << " occurs " << pair.second << " times\n";
    }
}

Python Code

1
2
3
4
5
6
7
8
9
def frequencyCount(arr):
    frequency_map = {}

    for num in arr:
        frequency_map[num] = frequency_map.get(num, 0) + 1

    # Print the frequencies
    for key, value in frequency_map.items():
        print(f"Number {key} occurs {value} times")

These code snippets demonstrate how to use a hash table to count the frequency of elements in an array. By using this approach, you can analyze the distribution of elements and use this information to solve various problems related to counting. The time complexity of this operation is (O(n)), where (n) is the number of elements in the array, making it an efficient way to perform counting operations.