Explore
ProGrad Programs
Placement Prep
TCS Codevita
Webinars
About Us

Edit
Reply




Edit

Program to find the frequency of each element of an array | FACE Prep

Published on 11 Mar 2020

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


C
C++
Java
Python 3

Output
Input - Enter the number of elements : 6 Enter elements in array : 1 2 3 1 2 6 Output - 1 occurs 2 times 2 occurs 2 times 3 occurs 1 times 6 occurs 1 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


C++

Output
Input - Enter the number of elements : 6 Enter elements in array : 1 2 3 1 2 6 Output - 1 occurs 2 times 2 occurs 2 times 3 occurs 1 times 6 occurs 1 times


Recommended Programs






If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in