Program to count the frequency of each element of an array.

Here, we have a basic program example to count the frequency of array elements using different languages. This program is created in c language, c++, Java, and Python.

Code to count the element frequency of array elements in C language

#include <stdio.h>
void main()
{
    int arr1[100], arr2[100];
    int n, i, j, ctr;

       printf("Input the number of elements to be stored in the array :");
       scanf("%d",&n);

       printf("Input %d elements in the array :\n",n);
       for(i=0;i<n;i++)
            {
	      printf("element - %d : ",i);
	      scanf("%d",&arr1[i]);
		  arr2[i] = -1;
	    }
    for(i=0; i<n; i++)
    {
        ctr = 1;
        for(j=i+1; j<n; j++)
        {
            if(arr1[i]==arr1[j])
            {
                ctr++;
                arr2[j] = 0;
            }
        }

        if(arr2[i]!=0)
        {
            arr2[i] = ctr;
        }
    }
    printf("\nThe frequency of all elements of array : \n");
    for(i=0; i<n; i++)
    {
        if(arr2[i]!=0)
        {
            printf("%d occurs %d times\n", arr1[i], arr2[i]);
        }
    }
}

Code to count the element frequency of array elements in C++ language

#include <iostream>
using namespace std;

int main()
{
   int arr1[100], arr2[100];
    int n, i, j, ctr;

       cout<<"Input the number of elements to be stored in the array : ";
       cin>>n;

       cout<<"Input "<<n<<" elements in the array : \n";
       for(i=0;i<n;i++)
            {
	      cout<<"element - "<<i<<" : ";
	      cin>>arr1[i];
		  arr2[i] = -1;
	    }
    for(i=0; i<n; i++)
    {
        ctr = 1;
        for(j=i+1; j<n; j++)
        {
            if(arr1[i]==arr1[j])
            {
                ctr++;
                arr2[j] = 0;
            }
        }

        if(arr2[i]!=0)
        {
            arr2[i] = ctr;
        }
    }
    cout<<"\nThe frequency of all elements of array : \n";
    for(i=0; i<n; i++)
    {
        if(arr2[i]!=0)
        {
            cout<<arr1[i]<<" occurs "<<arr2[i]<<" times\n";
        }
    }
}

Code to count the element frequency of array elements in Python language

arr1=[]
arr2=[]
n=int(input("Input the number of elements to be stored in the array : "))
print("Input ", n , " elements in the array :")
for i in range(0,n):
    l=int(input())
    arr1.append(l)

for i in range(0,n):
    flag = False
    count = 0
    for j in range(i+1,n):
        if(arr1[i]==arr1[j]):
            flag = True
            break
           
    if flag == True:
        continue
    
    for j in range(0,i+1):
        if(arr1[i]== arr1[j]):
            count += 1
   
    print("{0} occurs {1}".format(arr1[i],count)," times")

Code to count the element frequency of array elements in Java language

import java.util.*;
public class arrays
{
  public static void main(String[] args)
   {
     int n,ctr;
     Scanner sc = new Scanner(System.in);
     System.out.print("Input the number of elements to be stored in the array : ");
     n=sc.nextInt();

    int[] arr1 = new int[n];
    int[] arr2 = new int[n];

    System.out.println("Input "+n+" elements in the array :\n");
    for(int i=0; i<n; i++)
      {
       arr1[i]=sc.nextInt();
       arr2[i] = -1;
       }
     for(int i=0; i<n; i++)
    {
        ctr = 1;
        for(int j=i+1; j<n; j++)
        {
            if(arr1[i]==arr1[j])
            {
                ctr++;
                arr2[j] = 0;
            }
        }

        if(arr2[i]!=0)
        {
            arr2[i] = ctr;
        }
    }
     System.out.println("\nThe frequency of all elements of array : \n");
    for(int i=0; i<n; i++)
    {
        if(arr2[i]!=0)
        {
             System.out.println(arr1[i]+" occurs "+arr2[i]+" times\n");
        }
    }

   }
}