-
Notifications
You must be signed in to change notification settings - Fork 1
/
049FreqEachEleArr.cpp
72 lines (62 loc) · 1.57 KB
/
049FreqEachEleArr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//c++ program to count frequency of each element in an array
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
if (n <= 0)
{
cout << "Invalid array size. Please enter a positive integer." << endl;
return 1; // Exit with an error code
}
// Input elements into the array
for (int i = 0; i < n; i++)
{
cout << "Enter element " << i + 1 << ": ";
cin >> arr[i];
}
// Display the elements of the array
cout << "Elements in the array: |";
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout<<"|";
cout << endl;
// Initialize an array to store the frequency of each element
int frequency[n];
for (int i = 0; i < n; i++)
{
frequency[i] = -1; // Initialize frequencies to -1
}
// Calculate the frequency of each element
for (int i = 0; i < n; i++)
{
int count = 1;
for (int j = i + 1; j < n; j++)
{
if (arr[i] == arr[j])
{
count++;
frequency[j] = 0; // Mark as counted to avoid duplicate counting
}
}
if (frequency[i] != 0)
{
frequency[i] = count;
}
}
// Display the frequency of each element
cout << "Element\tFrequency" << endl;
for (int i = 0; i < n; i++)
{
if (frequency[i] != 0)
{
cout << arr[i] << "\t" << frequency[i] << endl;
}
}
return 0;
}