Frequency Measuring Techniques for Competitive Programming


In this article, we are going to find the different ways to find the frequency of numbers present in an array []. These methods are very useful in doing competitive programming for different problems for different cases. Sometimes, calculating the frequency of elements whether it is numbers or alphabets presented in the array is a complicated task. Various algorithms like Searching, array, divide and conquer can be used to find the repeated elements defined in the array.

Note- Take an integer array.

Let's explore the article, to know how it can be solved by using Java programming language.

To show you some instances

Instance-1

If,

 Number of Elements in Arr[] = N = 4
Arr[] = [5, 7, 6, 7]

Then,

Number  Frequency
5     1
7     2
6     1
arr Index 0 1 2 3
Value 5 7 6 7
data Index 0 1 2 3
Value
freq Index 0 1 2 3
Value
Size 0
i = 0 found = FALSE
j = 0 j < size $\mathrm{\Rightarrow}$ 0 < 0 $\mathrm{\Rightarrow}$ FALSE $\mathrm{\Rightarrow}$ Break the Loop
IF (found == FALSE) $\mathrm{\Rightarrow}$ TRUE $\mathrm{\Rightarrow}$ Go Inside IF Condition data[size] = arr[i] $\mathrm{\Rightarrow}$ data[0] = arr[0] $\mathrm{\Rightarrow}$ data[0] = 5 freq[size] = 1 $\mathrm{\Rightarrow}$ freq[0] = 1 size += 1 $\mathrm{\Rightarrow}$ size = size + 1 $\mathrm{\Rightarrow}$ size = 0 + 1 $\mathrm{\Rightarrow}$ size = 1
arr Index 0 1 2 3
Value 5 7 6 7
data Index 0 1 2 3
Value 5
freq Index 0 1 2 3
Value 1
Size 1
i = 1 found = FALSE
j = 0 j <size $\mathrm{\Rightarrow}$ 0 < 1 $\mathrm{\Rightarrow}$ TRUE $\mathrm{\Rightarrow}$ Go Inside FOR Loop IF (arr[i] == data[j]) $\mathrm{\Rightarrow}$ (arr[1] == data[0]) $\mathrm{\Rightarrow}$ (7 == 5) $\mathrm{\Rightarrow}$ FALSE $\mathrm{\Rightarrow}$ GO OUTSIDE IF Condition
j = 1 j < size $\mathrm{\Rightarrow}$ 1 < 1 $\mathrm{\Rightarrow}$ FALSE $\mathrm{\Rightarrow}$ Break the Loop
IF (found == FALSE) $\mathrm{\Rightarrow}$ TRUE $\mathrm{\Rightarrow}$ Go Inside IF Condition data[size] = arr[i] $\mathrm{\Rightarrow}$ data[1] = arr[1] $\mathrm{\Rightarrow}$ data[1] = 7 freq[size] = 1 $\mathrm{\Rightarrow}$ freq[1] = 1 size += 1 $\mathrm{\Rightarrow}$ size = size + 1 $\mathrm{\Rightarrow}$ size = 1 + 1 $\mathrm{\Rightarrow}$ size = 2
arr Index 0 1 2 3
Value 5 7 6 7
data Index 0 1 2 3
Value 5 7
freq Index 0 1 2 3
Value 1 1
Size 2
i = 2 found = FALSE
j = 0 j < size $\mathrm{\Rightarrow}$ 0 < 2 $\mathrm{\Rightarrow}$ TRUE $\mathrm{\Rightarrow}$ Go Inside FOR Loop IF (arr[i] == data[j]) $\mathrm{\Rightarrow}$ (arr[2] == data[0]) $\mathrm{\Rightarrow}$ (6 == 5) $\mathrm{\Rightarrow}$ FALSE $\mathrm{\Rightarrow}$ GO OUTSIDE IF Condition
j = 1 j < size $\mathrm{\Rightarrow}$ 1 < 2 $\mathrm{\Rightarrow}$ TRUE $\mathrm{\Rightarrow}$ Go Inside FOR Loop IF (arr[i] == data[j]) $\mathrm{\Rightarrow}$ (arr[2] == data[1]) $\mathrm{\Rightarrow}$ (6 == 7) $\mathrm{\Rightarrow}$ FALSE $\mathrm{\Rightarrow}$ GO OUTSIDE IF Condition
j = 2 j < size $\mathrm{\Rightarrow}$ 2 < 2 $\mathrm{\Rightarrow}$ FALSE $\mathrm{\Rightarrow}$ Break the Loop
IF (found == FALSE) $\mathrm{\Rightarrow}$ TRUE $\mathrm{\Rightarrow}$ Go Inside IF Condition data[size] = arr[i] $\mathrm{\Rightarrow}$ data[2] = arr[2] $\mathrm{\Rightarrow}$ data[2] = 6 freq[size] = 1 $\mathrm{\Rightarrow}$ freq[2] = 1 size += 1 $\mathrm{\Rightarrow}$ size = size + 1 $\mathrm{\Rightarrow}$ size = 2 + 1 $\mathrm{\Rightarrow}$ size = 3
arr Index 0 1 2 3
Value 5 7 6 7
data Index 0 1 2 3
Value 5 7 6
freq Index 0 1 2 3
Value 1 1 1
Size 3
i = 3 found = FALSE
j = 0 j < size $\mathrm{\Rightarrow}$ 0 < 3 $\mathrm{\Rightarrow}$ TRUE $\mathrm{\Rightarrow}$ Go Inside FOR Loop IF (arr[i] == data[j]) $\mathrm{\Rightarrow}$ (arr[3] == data[0]) $\mathrm{\Rightarrow}$ (7 == 5) $\mathrm{\Rightarrow}$ FALSE $\mathrm{\Rightarrow}$ GO OUTSIDE IF Condition
j = 1 j < size $\mathrm{\Rightarrow}$ 1 < 3 $\mathrm{\Rightarrow}$ TRUE $\mathrm{\Rightarrow}$ Go Inside FOR Loop IF (arr[i] == data[j]) $\mathrm{\Rightarrow}$ (arr[3] == data[1]) $\mathrm{\Rightarrow}$ (7 == 7) $\mathrm{\Rightarrow}$ TRUE $\mathrm{\Rightarrow}$ GO INSIDE IF Condition freq[j] += 1 $\mathrm{\Rightarrow}$ freq[1] += 1 $\mathrm{\Rightarrow}$ freq[1] = freq[1] + 1 $\mathrm{\Rightarrow}$ freq[1] = 1 + 1 = 2 found = TRUE BREAK THE LOOP
IF (found == FALSE) $\mathrm{\Rightarrow}$ FALSE $\mathrm{\Rightarrow}$ Go Outside IF Condition
arr Index 0 1 2 3
Value 5 7 6 7
data Index 0 1 2 3
Value 5 7 6
freq Index 0 1 2 3
Value 1 2 1
Size 3
i = 4 i < N $\mathrm{\Rightarrow}$ 4 < 4 $\mathrm{\Rightarrow}$ FALSE $\mathrm{\Rightarrow}$ BREAK The LOOP

Instance-2

If,

 Number of Elements in Arr[] = N = 8
Arr[] = [6, 7, 8, 9, 5, 4, 3, 5]

Then,

Number  Frequency
 3      1
 4      1
 5      2
 6      1
 7      1
 8      1
 9      1

Syntax

To store the frequency of the elements, there is an inbuilt Map interface.

Below refers to the syntax of it-

map<int, int> freqCounter;

here, the frequency counter for storing the frequency, the first element is “KEY” and the second element is “VALUE,” corresponding to the “KEY”.

Multiple Approaches:

We have provided the solution in different approaches.

  • By two arrays named data[] & freq[] mapped by their index.

  • By using a “map” for storing the data & frequency.

Approach-1: By using two arrays data[] & freq[] mapped by their index

In this algorithm, two different arrays, data[] and freq[], are used. data[] is used for storing the unique elements, and freq[] is used for storing the frequency. In this case, data[] and freq[] array are mapped with the help of an index.

Algorithm 1

Step-1: From the user, ask N (Number of Elements present in the array).

Step-2: Ask N-Elements for arr[] from the user.

Step 3: Create two Integer arrays named data[N], and freq[N].

Step 4: Create Integer variable “size”.

Step 5: OUTER LOOP (i=0 to N-1)

Step 6: SET found = FALSE

Step 7: INNER LOOP (j=0 to size-1)

Step 8: IF arr[i] == data[j], THEN

Step 9: INCREMENT freq[j] BY 1

Step 10: SET found = TRUE

Step 11: BREAK INNER LOOP

Step 12: IF found == FALSE, THEN

Step 13: SET data[size] = arr[i]

Step 14: SET freq[size] = 1

Step 15: INCREMENT size BY 1

Step-16: DISPLAY data[] array and freq[] array.

Step-17: Exit

Example

#include <iostream>
using namespace std;

/**
* find and print the frequency
*
* @param arr: array is an integer array
* @param N: Number of elements present in arr[]
*/
void displayFreq(int arr[], int N) {
   // create 2 arrays
   // one for storing data
   // second for storing frequency
   int data[N], freq[N], size = 0;

   // traverse the array
   for (int i=0; i<N; ++i) {
     bool found = false;
     // check for arr[i] in data[] array
     for (int j=0; j<size; ++j) {
       if (arr[i] == data[j]) {
         freq[j] += 1;
         found = true;
         break;
       }
     }

     // if arr[i] not found in data[] array
     if (found == false) {
       data[size] = arr[i];
       freq[size] = 1;
       size += 1;
     }
   }

   // display the result
   cout << "Number\tFrequency\n";
   for (int i=0; i<size; ++i) {
     cout << data[i]<< "\t" << freq[i] << endl;
   }
}

int main() {
   // declare N, and arr[]
   int N, arr[10000];

   cout << "Enter the required number: ";
   cin >> N;

   // Input elements in the array
   cout << "Enter " << N << " Numbers separated by SPACE: ";
   for (int i=0; i<N; ++i) {
     cin >> arr[i];
   }

   // call the function
   displayFreq(arr, N);

   return 0;
}

Output

Enter the required number: 7
Enter 7 Numbers separated by SPACE: 9 5 4 9 7 5 3
Number  Frequency
9     2
5     2
4     1
7     1
3     1

Approach-2: By using a “map” for storing the data & frequency.

This algorithm uses a “map” data type for storing the data and frequency in the form of KEY-VALUE Pairs.

Algorithm

Step-1: From the user, ask N (Number of Elements present in the array).

Step-2: Ask N-Elements for arr[] from the user.

Step-3: Create a Map for storing the frequency

Step-4: For each element of arr[], increase frequency counter in created Map

Step-5: Display the elements from their frequency in the console.

Step-6: Exit

Example

#include <iostream>
#include <map>

using namespace std;

/**
* find and print the frequency
*
* @param arr: array is an integer array
* @param N: Number of elements present in arr[]
*/
void displayFrequency(int arr[], int N) {
   // create map with the name freqCounter
   map<int, int> freqCounter;

   // traverse each element of array
   for (int i=0; i<N; ++i) {
     // check for arr[i] in map "freqCounter"
     if (freqCounter.find(arr[i]) == freqCounter.end()) {
       freqCounter.insert({arr[i], 1});
     } else {
       freqCounter[arr[i]] += 1;
     }
   }

   // display the result
   cout << "Number\tFrequency\n";
   std::map<int, int>::iterator ptr = freqCounter.begin();
   while (ptr != freqCounter.end()) {
     cout << ptr->first << "\t" << ptr->second << "\n";
     ++ptr;
   }
}

int main() {
   // declare N, and arr[]
   int N, arr[10000];

   cout << "Enter the required number: ";
   cin >> N;

   // Input elements in the array
   cout << "Enter " << N << " Numbers separated by SPACE: ";
   for (int i=0; i<N; ++i) {
     cin >> arr[i];
   }

   // call the function
   displayFrequency(arr, N);

   return 0;
}

Output

Enter the required number: 10
Enter 10 Numbers separated by SPACE: 5 6 7 9 8 5 4 3 6 5
Number  Frequency
3     1
4     1
5     3
6     2
7     1
8     1
9     1

This problem is solved by using three different approaches for different type of tasks based on the requirement. This article illustrates the computation of the frequency of items presented in the array.

Updated on: 28-Aug-2023

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements