0% found this document useful (0 votes)
54 views4 pages

Experiment Title: Counting Frequencies of Array Elements

The document summarizes an experiment on counting the frequencies of elements in an array. It includes the aim, tasks, algorithms, code, observations, and output. The algorithms traverse the input array and use either another array or hash table to store the frequency of each unique element. The hash table approach has a time complexity of O(n) while the other array method is O(n^2), where n is the size of the input array. The output prints each unique element and its frequency.

Uploaded by

Shreyansh Raut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views4 pages

Experiment Title: Counting Frequencies of Array Elements

The document summarizes an experiment on counting the frequencies of elements in an array. It includes the aim, tasks, algorithms, code, observations, and output. The algorithms traverse the input array and use either another array or hash table to store the frequency of each unique element. The hash table approach has a time complexity of O(n) while the other array method is O(n^2), where n is the size of the input array. The output prints each unique element and its frequency.

Uploaded by

Shreyansh Raut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment Title

Student Name: Shreyansh Raut UID:18BCS1703


Branch: CSE Section/Group- 2[A]
Semester: 5 Date of Performance:29/7/2020
Subject Name: DAA LAB SUBJECT CODE: CSP-309

Aim/Overview of the practical:Counting frequencies of array elements

2. Task to be done/ Which logistics used:Given an array which may contain duplicates, print all elements
and their frequencies.

3. Algorithm/Flowchart
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.

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

4. Steps for experiment/practical/Code:


#include<iostream>
#include<unordered_map>
using namespace std;

void countFreq(int a[],int n)


{
unordered_map<int ,int>fre;
for(int i=0;i<n;i++)
fre[a[i]]++;

for (auto x: fre)


cout<<x.first<<" "<<x.second<<endl;
}
int main()
{
int n;
cout<<"Enter the no. of elements:";
cin>>n;
int a[n],i;
cout<<"Enter the elements:"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
countFreq(a,n);
return 0;
}

5. Observations/Discussions/ Complexity Analysis:


Time complexity of array method is O(n^2) and of hashing is O(n), where n is the upper limit of a and b

6. Result/Output/Writing Summary:
Output:

Enter the no. of elements:5


Enter the elements:
4
2
6
6
2

22
41
62

Learning outcomes (What I have learnt):


1.To find time complexity
2. To translate algo into code format
3. To use algo to count the frequencies of the repeated numbers in an array.
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

You might also like