-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReaptingElements.c
More file actions
66 lines (64 loc) · 1.62 KB
/
ReaptingElements.c
File metadata and controls
66 lines (64 loc) · 1.62 KB
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
/*Aim of the program: Write a program to read ‘n’ integers from a disc file that must contain
some duplicate values and store them into an array. Perform the following operations on the
array.
a) Find out the total number of duplicate elements.
b) Find out the most repeating element in the array.*/
#include <stdio.h>
int main()
{
FILE *file;
int n, i, j;
int countDuplicates = 0;
int maxCount = 0;
int mostRepeatingElement;
int numArray[100];
int frequency[100] = {0};
file = fopen("input.txt", "r");
if (file == NULL)
{
printf("Error\n");
return 1;
}
printf("Enter the number of numbers you want to read from file: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
fscanf(file, "%d", &numArray[i]);
}
fclose(file);
printf("The array: ");
for (i = 0; i < n; i++)
{
printf("%d ", numArray[i]);
}
printf("\n");
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (numArray[i] == numArray[j])
{
frequency[i]++;
}
}
}
for (i = 0; i < n; i++)
{
if (frequency[i] > 0)
{
countDuplicates++;
}
if (frequency[i] > maxCount)
{
maxCount = frequency[i];
mostRepeatingElement = numArray[i];
}
else
{
printf("No element found.");
}
}
printf("Total number of duplicate values = %d\n", countDuplicates);
printf("The most repeating element in the array = %d\n", mostRepeatingElement);
return 0;
}