Often we have a requirement to find elements that are repeating in an array. So let's see a very simple program to find a repeated element in an array.
Code:
#include
#include
void Count_Repeated(int * arr, int size);
int main() {
int arr1[8] = {2,4,2,5,6,4,5,2};
Count_Repeated(arr1, 8);
}
void Count_Repeated(int * arr, int size) {
int i, j, count;
for (i = 0; i < size; i++) {
count = 1;
for (j = i + 1; j < size; j++) {
if (arr[i] == arr[j] && arr[i] != 0) {
count++;
arr[j] = 0;
}
}
if (count > 1 && arr[i] != 0) printf("%d repeated %d times \n", arr[i], count);
}
}
Output:
2 repeated 3 times
4 repeated 2 times
5 repeated 2 times