-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_23.c
More file actions
36 lines (29 loc) · 818 Bytes
/
problem_23.c
File metadata and controls
36 lines (29 loc) · 818 Bytes
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
// Problem 23: Count the frequency of each element of an array
#include <stdio.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
int visited[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
visited[i] = 0; // Initialize visited array
}
printf("Element frequencies:\n");
for (int i = 0; i < n; i++) {
if (visited[i] == 1) {
continue; // Skip if already counted
}
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
visited[j] = 1; // Mark as visited
}
}
printf("%d occurs %d time(s)\n", arr[i], count);
}
return 0;
}