-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path07_count_duplicates.c
142 lines (117 loc) · 3.38 KB
/
07_count_duplicates.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// // Write a function in C to count a total number of duplicate elements in an array.
// // Header Files
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
// // Functions Declarations (Prototypes)
void printArray(int[], int);
void inputArray(int[], int);
int countTotalDuplicates(int[], int);
void sortAsc(int[], int);
int linearSearch(int[], int, int);
void copyArray(int[], int, int[]);
// // Main Function Start
int main()
{
const int ARRAY_SIZE;
printf("\nHow Many Elements You Want to Enter => ");
scanf("%d", &ARRAY_SIZE);
// // Check for Invalid Array Size
if (ARRAY_SIZE < 1)
{
puts("\n!!! Invalid Input, Plz Correctly Specify Number of Elements...");
exit(0);
}
// // Declare Array of Variable size
int arr[ARRAY_SIZE], isAdjDupPresent;
// // Input Elements
printf("\nEnter %d Elements => ", ARRAY_SIZE);
inputArray(arr, ARRAY_SIZE);
// // Print Array
puts("\n\n>>>>>>>> Entered Elements <<<<<<<<<");
printArray(arr, ARRAY_SIZE);
// // find first occurrence of adjacent duplicate
int totalDup = countTotalDuplicates(arr, ARRAY_SIZE);
printf("\nThere Are %d Duplicate Elements in Array", totalDup);
putch('\n');
getch();
return 0;
}
// // Main Function End
// // Functions Definitions 👇👇
// // Function to Display Array Elements
void printArray(int arr[], int size)
{
putch('\n'); // // Add new line
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
putch('\n'); // // Add new line
}
// // Function to Input Array Elements
void inputArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
scanf("%d", &arr[i]);
}
// // Function to Find the Total Number of Duplicate Elements In Array
int countTotalDuplicates(int arr[], int size)
{
// // 1st Approach (using sorting)
int countDup = 0, copyOfArr[size];
copyArray(arr, size, copyOfArr); // // copy arr into copyOfArr
sortAsc(copyOfArr, size); // // sort array
for (int i = 0; i < size - 1; i += countDup ? countDup : 1)
{
for (int j = i + 1; copyOfArr[i] == copyOfArr[j]; j++)
countDup++;
}
return countDup;
// // 2nd Approach
// // int inspected[size], k = 0, countDup = 0;
// // for (int i = 0; i < size; i++)
// // {
// // if (linearSearch(inspected, k, arr[i]) == -1)
// // {
// // for (int j = 0; j < size; j++)
// // {
// // if (arr[i] == arr[j] && i != j)
// // countDup++;
// // }
// // inspected[k++] = arr[i];
// // }
// // }
// // return countDup;
}
// // Function to Sort an Array in Ascending Order
void sortAsc(int arr[], int size)
{
// // Bubble Sort
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - 1; j++)
{
if (arr[j] < arr[j + 1]) // // true, then swap
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// // Function to Search An Element Using Linear Search
int linearSearch(int arr[], int size, int search)
{
for (int i = 0; i < size; i++)
{
if (arr[i] == search)
return i;
}
return -1;
}
// // Function to Copy Array
void copyArray(int arr[], int size, int arr2[])
{
for (int i = 0; i < size; i++)
arr2[i] = arr[i];
}