Program to find the frequency of each element of an array | faceprep

Program to find the frequency of each element of an array is discussed here. Given an array, the task is to find the frequency of all the elements of the array.

For example, consider the following array

Input:

arr = {1, 2, 3, 5, 2, 9, 7, 3, 5}

Output: 

1 occurs 1 times
2 occurs 2 times
3 occurs 2 times
5 occurs 2 times
7 occurs 1 times
9 occurs 1 times

Algorithm to find the frequency of each element of an array

  • Input the number of elements of an array.
  • Input the array elements.
  • Create another array to store the frequency of elements.
  • Traverse the input array and update the count of the elements in the frequency array.
  • Print the frequency array which displays the frequency of all the elements of the array.

Program to find the frequency of each element of an array is given below.

/* C program to find the frequency of each element of array */

#include <stdio.h>

int main()
{
int arr[100], freq[100];
int size, i, j, count;

/* Input size of array */
printf(“\nEnter size of array: “);
scanf(“%d”, &size);

/* Input elements in array */
printf(“\nEnter elements in array: “);
for(i=0; i<size; i++)
{
scanf(“%d”, &arr[i]);

/* Initially initialize frequencies to -1 */
freq[i] = -1;
}


for(i=0; i<size; i++)
{
count = 1;
for(j=i+1; j<size; j++)
{
/* If duplicate element is found */
if(arr[i]==arr[j])
{
count++;

/* Make sure not to count frequency of same element again */
freq[j] = 0;
}
}

/* If frequency of current element is not counted */
if(freq[i] != 0)
{
freq[i] = count;
}
}

/* Print frequency of each element */
printf(“\nFrequency of all elements of array : \n”);
for(i=0; i<size; i++)
{
if(freq[i] != 0)
{
printf(“%d occurs %d times\n”, arr[i], freq[i]);
}
}
}

/* C++ program to find the frequency of each element of array */

#include <iostream>
using namespace std;

int main()
{
int freq[100];
int size, i, j, count;

/* Input size of array */
cout << “\nEnter size of array: “;
cin >> size;

int arr[size];

/* Input elements in array */
cout << “\nEnter elements in array: “;
for(i=0; i<size; i++)
{
cin >> arr[i];

/* Initially initialize frequencies to -1 */
freq[i] = -1;
}


for(i=0; i<size; i++)
{
count = 1;
for(j=i+1; j<size; j++)
{
/* If duplicate element is found */
if(arr[i]==arr[j])
{
count++;

/* Make sure not to count frequency of same element again */
freq[j] = 0;
}
}

/* If frequency of current element is not counted */
if(freq[i] != 0)
{
freq[i] = count;
}
}

/* Print frequency of each element */
cout << “\nFrequency of all elements of array : \n”;
for(i=0; i<size; i++)
{
if(freq[i] != 0)
{
cout << arr[i] << ” occurs ” << freq[i] << ” times ” << endl;
}
}
}

/* Java program to find the frequency of each element of an array */
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[] arr = new int[100];
int[] freq = new int[100];
int size, i, j, count;

/* Input size of array */
System.out.print(“\nEnter size of array: “);
size = sc.nextInt();

/* Input elements in array */
System.out.print(“\nEnter elements in array: “);
for(i=0; i<size; i++)
{
arr[i] = sc.nextInt();

/* Initially initialize frequencies to -1 */
freq[i] = -1;
}


for(i=0; i<size; i++)
{
count = 1;
for(j=i+1; j<size; j++)
{
/* If duplicate element is found */
if(arr[i]==arr[j])
{
count++;

/* Make sure not to count frequency of same element again */
freq[j] = 0;
}
}

/* If frequency of current element is not counted */
if(freq[i] != 0)
{
freq[i] = count;
}
}

/* Print frequency of each element */
System.out.print(“\nFrequency of all elements of array : \n”);
for(i=0; i<size; i++)
{
if(freq[i] != 0)
{
System.out.print(arr[i] + ” occurs ” + freq[i] + ” times” + “\n”);
}
}

}
}

# Python program to find the frequency of each element of an array

# Input size of array
n = int(input(“Enter the number of elements : “))
arr = []
freq = [-1] * n
# Input elements in array
print(“\nEnter elements in array: “)
for i in range(n):
temp = int(input())
# Initially initialize frequencies to -1
arr.append(temp)
for i in range(0, n):
count = 1
for j in range(i+1, n):
if(arr[i] == arr[j]):
count = count + 1
freq[j] = 0

if(freq[i] != 0):
freq[i] = count

# Print frequency of each element
print(“\nFrequency of all elements of array : \n”);
for i in range(0, n):
if(freq[i] != 0):
print(arr[i], ” occurs “, freq[i], ” times”,)

Algorithm to find the frequency of each element of an array using hashing

  • Input the number of elements of the array.
  • Input the array elements.
  • Create a hash table and update the element in one column and its frequency in the other column.
  • Print the element along with its frequency.

Program to find the frequency of each element of an array using hashing is given below.

/* C++ program to find the frequency of each element of an array using hashing

#include <bits/stdc++.h>
using namespace std;

void countFreq(int arr[], int n)
{
unordered_map<int, int> mp;

// Traverse through array elements and count frequencies
for (int i = 0; i < n; i++)
mp[arr[i]]++;

// Traverse through map and print frequencies
for (auto x : mp)
cout << x.first << ” occurs ” << x.second << ” times ” << endl;
}

int main()
{
int n;
cout << “\nEnter the number of elements : “;
cin >> n;
cout << “\nInput the array elements : “;
int arr[n];
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
countFreq(arr, n);
return 0;
}

Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Output:

frequency of each element of an array